VirtualBox

source: vbox/trunk/src/VBox/Devices/VirtIO/VirtioCore.h@ 94275

Last change on this file since 94275 was 94275, checked in by vboxsync, 3 years ago

VirtioCore: "Transitional devices MUST have a PCI Revision ID of 0."
"Non-transitional devices SHOULD have a PCI Revision ID of 1 or
higher." NetBSD (strictly speaking incorrectly) insists on revision
being 1 for non-transitional devices (device id > 0x1040), so obey the
"SHOULD" which we don't really have any reason not to. bugref:9440.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 66.1 KB
Line 
1/* $Id: VirtioCore.h 94275 2022-03-16 17:41:07Z vboxsync $ */
2
3/** @file
4 * VirtioCore.h - Virtio Declarations
5 */
6
7/*
8 * Copyright (C) 2009-2022 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#ifndef VBOX_INCLUDED_SRC_VirtIO_VirtioCore_h
20#define VBOX_INCLUDED_SRC_VirtIO_VirtioCore_h
21#ifndef RT_WITHOUT_PRAGMA_ONCE
22# pragma once
23#endif
24
25#include <iprt/ctype.h>
26#include <iprt/sg.h>
27#include <iprt/types.h>
28
29#ifdef LOG_ENABLED
30# define VIRTIO_HEX_DUMP(logLevel, pv, cb, base, title) \
31 do { \
32 if (LogIsItEnabled(logLevel, LOG_GROUP)) \
33 virtioCoreHexDump((pv), (cb), (base), (title)); \
34 } while (0)
35#else
36# define VIRTIO_HEX_DUMP(logLevel, pv, cb, base, title) do { } while (0)
37#endif
38
39/** Marks the start of the virtio saved state (just for sanity). */
40#define VIRTIO_SAVEDSTATE_MARKER UINT64_C(0x1133557799bbddff)
41
42/** Pointer to the shared VirtIO state. */
43typedef struct VIRTIOCORE *PVIRTIOCORE;
44/** Pointer to the ring-3 VirtIO state. */
45typedef struct VIRTIOCORER3 *PVIRTIOCORER3;
46/** Pointer to the ring-0 VirtIO state. */
47typedef struct VIRTIOCORER0 *PVIRTIOCORER0;
48/** Pointer to the raw-mode VirtIO state. */
49typedef struct VIRTIOCORERC *PVIRTIOCORERC;
50/** Pointer to the instance data for the current context. */
51typedef CTX_SUFF(PVIRTIOCORE) PVIRTIOCORECC;
52
53#define VIRTIO_MAX_VIRTQ_NAME_SIZE 32 /**< Maximum length of a queue name */
54#define VIRTQ_SIZE 1024 /**< Max size (# entries) of a virtq */
55#define VIRTQ_MAX_COUNT 24 /**< Max queues we allow guest to create */
56#define VIRTIO_NOTIFY_OFFSET_MULTIPLIER 2 /**< VirtIO Notify Cap. MMIO config param */
57#define VIRTIO_REGION_LEGACY_IO 0 /**< BAR for VirtIO legacy drivers MBZ */
58#define VIRTIO_REGION_PCI_CAP 2 /**< BAR for VirtIO Cap. MMIO (impl specific) */
59#define VIRTIO_REGION_MSIX_CAP 0 /**< Bar for MSI-X handling */
60#define VIRTIO_PAGE_SIZE 4096 /**< Page size used by VirtIO specification */
61
62/**
63 * @todo Move the following virtioCoreGCPhysChain*() functions mimic the functionality of the related
64 * into some VirtualBox source tree common location and out of this code.
65 *
66 * They behave identically to the S/G utilities in the RT library, except they work with that
67 * GCPhys data type specifically instead of void *, to avoid potentially disastrous mismatch
68 * between sizeof(void *) and sizeof(GCPhys).
69 *
70 */
71typedef struct VIRTIOSGSEG /**< An S/G entry */
72{
73 RTGCPHYS GCPhys; /**< Pointer to the segment buffer */
74 size_t cbSeg; /**< Size of the segment buffer */
75} VIRTIOSGSEG;
76
77typedef VIRTIOSGSEG *PVIRTIOSGSEG, **PPVIRTIOSGSEG;
78typedef const VIRTIOSGSEG *PCVIRTIOSGSEG;
79
80typedef struct VIRTIOSGBUF
81{
82 PVIRTIOSGSEG paSegs; /**< Pointer to the scatter/gather array */
83 unsigned cSegs; /**< Number of segs in scatter/gather array */
84 unsigned idxSeg; /**< Current segment we are in */
85 RTGCPHYS GCPhysCur; /**< Ptr to byte within the current seg */
86 size_t cbSegLeft; /**< # of bytes left in the current segment */
87} VIRTIOSGBUF;
88
89typedef VIRTIOSGBUF *PVIRTIOSGBUF, **PPVIRTIOSGBUF;
90typedef const VIRTIOSGBUF *PCVIRTIOSGBUF;
91
92/**
93 * VirtIO buffers are descriptor chains (e.g. scatter-gather vectors). A VirtIO buffer is referred to by the index
94 * of its head descriptor. Each descriptor optionally chains to another descriptor, and so on.
95 *
96 * For any given descriptor, each length and GCPhys pair in the chain represents either an OUT segment (e.g. guest-to-host)
97 * or an IN segment (host-to-guest).
98 *
99 * A VIRTQBUF is created and retured from a call to to either virtioCoreR3VirtqAvailBufPeek() or virtioCoreR3VirtqAvailBufGet().
100 *
101 * Those functions consolidate the VirtIO descriptor chain into a single representation where:
102 *
103 * pSgPhysSend GCPhys s/g buffer containing all of the (VirtIO) OUT descriptors
104 * pSgPhysReturn GCPhys s/g buffer containing all of the (VirtIO) IN descriptors
105 *
106 * The OUT descriptors are data sent from guest to host (dev-specific commands and/or data)
107 * The IN are to be filled with data (converted to physical) on host, to be returned to guest
108 *
109 */
110typedef struct VIRTQBUF
111{
112 uint32_t u32Magic; /**< Magic value, VIRTQBUF_MAGIC. */
113 uint16_t uVirtq; /**< VirtIO index of associated virtq */
114 uint16_t pad;
115 uint32_t volatile cRefs; /**< Reference counter. */
116 uint32_t uHeadIdx; /**< Head idx of associated desc chain */
117 size_t cbPhysSend; /**< Total size of src buffer */
118 PVIRTIOSGBUF pSgPhysSend; /**< Phys S/G buf for data from guest */
119 size_t cbPhysReturn; /**< Total size of dst buffer */
120 PVIRTIOSGBUF pSgPhysReturn; /**< Phys S/G buf to store result for guest */
121
122 /** @name Internal (bird combined 5 allocations into a single), fingers off.
123 * @{ */
124 VIRTIOSGBUF SgBufIn;
125 VIRTIOSGBUF SgBufOut;
126 VIRTIOSGSEG aSegsIn[VIRTQ_SIZE];
127 VIRTIOSGSEG aSegsOut[VIRTQ_SIZE];
128 /** @} */
129} VIRTQBUF_T;
130
131/** Pointers to a Virtio descriptor chain. */
132typedef VIRTQBUF_T *PVIRTQBUF, **PPVIRTQBUF;
133
134/** Magic value for VIRTQBUF_T::u32Magic. */
135#define VIRTQBUF_MAGIC UINT32_C(0x19600219)
136
137typedef struct VIRTIOPCIPARAMS
138{
139 uint16_t uDeviceId; /**< PCI Cfg Device ID */
140 uint16_t uClassBase; /**< PCI Cfg Base Class */
141 uint16_t uClassSub; /**< PCI Cfg Subclass */
142 uint16_t uClassProg; /**< PCI Cfg Programming Interface Class */
143 uint16_t uSubsystemId; /**< PCI Cfg Card Manufacturer Vendor ID */
144 uint16_t uInterruptLine; /**< PCI Cfg Interrupt line */
145 uint16_t uInterruptPin; /**< PCI Cfg Interrupt pin */
146} VIRTIOPCIPARAMS, *PVIRTIOPCIPARAMS;
147
148
149/* Virtio Platform Independent Reserved Feature Bits (see 1.1 specification section 6) */
150
151#define VIRTIO_F_NOTIFY_ON_EMPTY RT_BIT_64(24) /**< Legacy feature: Force intr if no AVAIL */
152#define VIRTIO_F_ANY_LAYOUT RT_BIT_64(27) /**< Doc bug: Goes under two names in spec */
153#define VIRTIO_F_RING_INDIRECT_DESC RT_BIT_64(28) /**< Doc bug: Goes under two names in spec */
154#define VIRTIO_F_INDIRECT_DESC RT_BIT_64(28) /**< Allow descs to point to list of descs */
155#define VIRTIO_F_RING_EVENT_IDX RT_BIT_64(29) /**< Doc bug: Goes under two names in spec */
156#define VIRTIO_F_EVENT_IDX RT_BIT_64(29) /**< Allow notification disable for n elems */
157#define VIRTIO_F_BAD_FEATURE RT_BIT_64(30) /**< QEMU kludge. UNUSED as of >= VirtIO 1.0 */
158#define VIRTIO_F_VERSION_1 RT_BIT_64(32) /**< Required feature bit for 1.0 devices */
159#define VIRTIO_F_ACCESS_PLATFORM RT_BIT_64(33) /**< Funky guest mem access (VirtIO 1.1 NYI) */
160#define VIRTIO_F_RING_PACKED RT_BIT_64(34) /**< Packed Queue Layout (VirtIO 1.1 NYI) */
161#define VIRTIO_F_IN_ORDER RT_BIT_64(35) /**< Honor guest buf order (VirtIO 1.1 NYI) */
162#define VIRTIO_F_ORDER_PLATFORM RT_BIT_64(36) /**< Host mem access honored (VirtIO 1.1 NYI) */
163#define VIRTIO_F_SR_IOV RT_BIT_64(37) /**< Dev Single Root I/O virt (VirtIO 1.1 NYI) */
164#define VIRTIO_F_NOTIFICAITON_DATA RT_BIT_64(38) /**< Driver passes extra data (VirtIO 1.1 NYI) */
165
166typedef struct VIRTIO_FEATURES_LIST
167{
168 uint64_t fFeatureBit;
169 const char *pcszDesc;
170} VIRTIO_FEATURES_LIST, *PVIRTIO_FEATURES_LIST;
171
172static const VIRTIO_FEATURES_LIST s_aCoreFeatures[] =
173{
174 { VIRTIO_F_VERSION_1, " VERSION_1 Guest driver supports VirtIO specification V1.0+ (e.g. \"modern\")\n" },
175 { VIRTIO_F_RING_EVENT_IDX, " RING_EVENT_IDX Enables use_event and avail_event fields described in 2.4.7, 2.4.8\n" },
176 { VIRTIO_F_RING_INDIRECT_DESC, " RING_INDIRECT_DESC Driver can use descriptors with VIRTQ_DESC_F_INDIRECT flag set\n" },
177};
178
179#define VIRTIO_DEV_INDEPENDENT_FEATURES_OFFERED ( 0 ) /**< TBD: Add VIRTIO_F_INDIRECT_DESC */
180#define VIRTIO_DEV_INDEPENDENT_LEGACY_FEATURES_OFFERED ( 0 ) /**< Only offered to legacy drivers */
181
182#define VIRTIO_ISR_VIRTQ_INTERRUPT RT_BIT_32(0) /**< Virtq interrupt bit of ISR register */
183#define VIRTIO_ISR_DEVICE_CONFIG RT_BIT_32(1) /**< Device configuration changed bit of ISR */
184#define DEVICE_PCI_NETWORK_SUBSYSTEM 1 /**< Network Card, per VirtIO legacy spec. */
185#define DEVICE_PCI_REVISION_ID_VIRTIO_TRANS 0 /**< VirtIO Transitional device revision (MBZ) */
186#define DEVICE_PCI_REVISION_ID_VIRTIO_V1 1 /**< VirtIO device revision (SHOULD be >= 1) */
187
188#define DEVICE_PCI_VENDOR_ID_VIRTIO 0x1AF4 /**< Guest driver locates dev via (mandatory) */
189
190/**
191 * Start of the PCI device id range for non-transitional devices.
192 *
193 * "Devices ... have the PCI Device ID calculated by adding 0x1040 to
194 * the Virtio Device ID, as indicated in section [Device Types]. ...
195 * Non-transitional devices SHOULD have a PCI Device ID in the range
196 * 0x1040 to 0x107f.
197 */
198#define DEVICE_PCI_DEVICE_ID_VIRTIO_BASE 0x1040
199
200/** Reserved (*negotiated*) Feature Bits (e.g. device independent features, VirtIO 1.0 spec,section 6) */
201
202#define VIRTIO_MSI_NO_VECTOR 0xffff /**< Vector value to disable MSI for queue */
203
204/** Device Status field constants (from Virtio 1.0 spec) */
205#define VIRTIO_STATUS_ACKNOWLEDGE 0x01 /**< Guest driver: Located this VirtIO device */
206#define VIRTIO_STATUS_DRIVER 0x02 /**< Guest driver: Can drive this VirtIO dev. */
207#define VIRTIO_STATUS_DRIVER_OK 0x04 /**< Guest driver: Driver set-up and ready */
208#define VIRTIO_STATUS_FEATURES_OK 0x08 /**< Guest driver: Feature negotiation done */
209#define VIRTIO_STATUS_FAILED 0x80 /**< Guest driver: Fatal error, gave up */
210#define VIRTIO_STATUS_DEVICE_NEEDS_RESET 0x40 /**< Device experienced unrecoverable error */
211
212typedef enum VIRTIOVMSTATECHANGED
213{
214 kvirtIoVmStateChangedInvalid = 0,
215 kvirtIoVmStateChangedReset,
216 kvirtIoVmStateChangedSuspend,
217 kvirtIoVmStateChangedPowerOff,
218 kvirtIoVmStateChangedResume,
219 kvirtIoVmStateChangedFor32BitHack = 0x7fffffff
220} VIRTIOVMSTATECHANGED;
221
222/** @def Virtio Device PCI Capabilities type codes */
223#define VIRTIO_PCI_CAP_COMMON_CFG 1 /**< Common configuration PCI capability ID */
224#define VIRTIO_PCI_CAP_NOTIFY_CFG 2 /**< Notification area PCI capability ID */
225#define VIRTIO_PCI_CAP_ISR_CFG 3 /**< ISR PCI capability id */
226#define VIRTIO_PCI_CAP_DEVICE_CFG 4 /**< Device-specific PCI cfg capability ID */
227#define VIRTIO_PCI_CAP_PCI_CFG 5 /**< PCI CFG capability ID */
228
229#define VIRTIO_PCI_CAP_ID_VENDOR 0x09 /**< Vendor-specific PCI CFG Device Cap. ID */
230
231/**
232 * The following is the PCI capability struct common to all VirtIO capability types
233 */
234typedef struct virtio_pci_cap
235{
236 /* All little-endian */
237 uint8_t uCapVndr; /**< Generic PCI field: PCI_CAP_ID_VNDR */
238 uint8_t uCapNext; /**< Generic PCI field: next ptr. */
239 uint8_t uCapLen; /**< Generic PCI field: capability length */
240 uint8_t uCfgType; /**< Identifies the structure. */
241 uint8_t uBar; /**< Where to find it. */
242 uint8_t uPadding[3]; /**< Pad to full dword. */
243 uint32_t uOffset; /**< Offset within bar. (L.E.) */
244 uint32_t uLength; /**< Length of struct, in bytes. (L.E.) */
245} VIRTIO_PCI_CAP_T, *PVIRTIO_PCI_CAP_T;
246
247/**
248 * VirtIO Legacy Capabilities' related MMIO-mapped structs (see virtio-0.9.5 spec)
249 *
250 * Note: virtio_pci_device_cap is dev-specific, implemented by client. Definition unknown here.
251 */
252typedef struct virtio_legacy_pci_common_cfg
253{
254 /* Device-specific fields */
255 uint32_t uDeviceFeatures; /**< RO (device reports features to driver) */
256 uint32_t uDriverFeatures; /**< RW (driver-accepted device features) */
257 uint32_t uVirtqPfn; /**< RW (driver writes queue page number) */
258 uint16_t uQueueSize; /**< RW (queue size, 0 - 2^n) */
259 uint16_t uVirtqSelect; /**< RW (selects queue focus for these fields) */
260 uint16_t uQueueNotify; /**< RO (offset into virtqueue; see spec) */
261 uint8_t fDeviceStatus; /**< RW (driver writes device status, 0=reset) */
262 uint8_t fIsrStatus; /**< RW (driver writes ISR status, 0=reset) */
263#ifdef LEGACY_MSIX_SUPPORTED
264 uint16_t uMsixConfig; /**< RW (driver sets MSI-X config vector) */
265 uint16_t uMsixVector; /**< RW (driver sets MSI-X config vector) */
266#endif
267} VIRTIO_LEGACY_PCI_COMMON_CFG_T, *PVIRTIO_LEGACY_PCI_COMMON_CFG_T;
268
269/**
270 * VirtIO 1.0 Capabilities' related MMIO-mapped structs:
271 *
272 * Note: virtio_pci_device_cap is dev-specific, implemented by client. Definition unknown here.
273 */
274typedef struct virtio_pci_common_cfg
275{
276 /* Device-specific fields */
277 uint32_t uDeviceFeaturesSelect; /**< RW (driver selects device features) */
278 uint32_t uDeviceFeatures; /**< RO (device reports features to driver) */
279 uint32_t uDriverFeaturesSelect; /**< RW (driver selects driver features) */
280 uint32_t uDriverFeatures; /**< RW (driver-accepted device features) */
281 uint16_t uMsixConfig; /**< RW (driver sets MSI-X config vector) */
282 uint16_t uNumVirtqs; /**< RO (device specifies max queues) */
283 uint8_t fDeviceStatus; /**< RW (driver writes device status, 0=reset) */
284 uint8_t uConfigGeneration; /**< RO (device changes when changing configs) */
285
286 /* Virtq-specific fields (values reflect (via MMIO) info related to queue indicated by uVirtqSelect. */
287 uint16_t uVirtqSelect; /**< RW (selects queue focus for these fields) */
288 uint16_t uQueueSize; /**< RW (queue size, 0 - 2^n) */
289 uint16_t uMsixVector; /**< RW (driver selects MSI-X queue vector) */
290 uint16_t uEnable; /**< RW (driver controls usability of queue) */
291 uint16_t uNotifyOffset; /**< RO (offset into virtqueue; see spec) */
292 uint64_t GCPhysVirtqDesc; /**< RW (driver writes desc table phys addr) */
293 uint64_t GCPhysVirtqAvail; /**< RW (driver writes avail ring phys addr) */
294 uint64_t GCPhysVirtqUsed; /**< RW (driver writes used ring phys addr) */
295} VIRTIO_PCI_COMMON_CFG_T, *PVIRTIO_PCI_COMMON_CFG_T;
296
297typedef struct virtio_pci_notify_cap
298{
299 struct virtio_pci_cap pciCap; /**< Notification MMIO mapping capability */
300 uint32_t uNotifyOffMultiplier; /**< notify_off_multiplier */
301} VIRTIO_PCI_NOTIFY_CAP_T, *PVIRTIO_PCI_NOTIFY_CAP_T;
302
303typedef struct virtio_pci_cfg_cap
304{
305 struct virtio_pci_cap pciCap; /**< Cap. defines the BAR/off/len to access */
306 uint8_t uPciCfgData[4]; /**< I/O buf for above cap. */
307} VIRTIO_PCI_CFG_CAP_T, *PVIRTIO_PCI_CFG_CAP_T;
308
309/**
310 * PCI capability data locations (PCI CFG and MMIO).
311 */
312typedef struct VIRTIO_PCI_CAP_LOCATIONS_T
313{
314 uint16_t offMmio;
315 uint16_t cbMmio;
316 uint16_t offPci;
317 uint16_t cbPci;
318} VIRTIO_PCI_CAP_LOCATIONS_T;
319
320typedef struct VIRTQUEUE
321{
322 RTGCPHYS GCPhysVirtqDesc; /**< (MMIO) Addr of virtq's desc ring GUEST */
323 RTGCPHYS GCPhysVirtqAvail; /**< (MMIO) Addr of virtq's avail ring GUEST */
324 RTGCPHYS GCPhysVirtqUsed; /**< (MMIO) Addr of virtq's used ring GUEST */
325 uint16_t uMsixVector; /**< (MMIO) MSI-X vector GUEST */
326 uint16_t uEnable; /**< (MMIO) Queue enable flag GUEST */
327 uint16_t uNotifyOffset; /**< (MMIO) Notification offset for queue HOST */
328 uint16_t uQueueSize; /**< (MMIO) Size of queue HOST/GUEST */
329 uint16_t uAvailIdxShadow; /**< Consumer's position in avail ring */
330 uint16_t uUsedIdxShadow; /**< Consumer's position in used ring */
331 uint16_t uVirtq; /**< Index of this queue */
332 char szName[32]; /**< Dev-specific name of queue */
333 bool fUsedRingEvent; /**< Flags if used idx to notify guest reached */
334 bool fAttached; /**< Flags if dev-specific client attached */
335} VIRTQUEUE, *PVIRTQUEUE;
336
337/**
338 * The core/common state of the VirtIO PCI devices, shared edition.
339 */
340typedef struct VIRTIOCORE
341{
342 char szInstance[16]; /**< Instance name, e.g. "VIRTIOSCSI0" */
343 PPDMDEVINS pDevInsR0; /**< Client device instance */
344 PPDMDEVINS pDevInsR3; /**< Client device instance */
345 VIRTQUEUE aVirtqueues[VIRTQ_MAX_COUNT]; /**< (MMIO) VirtIO contexts for queues */
346 uint64_t uDeviceFeatures; /**< (MMIO) Host features offered HOST */
347 uint64_t uDriverFeatures; /**< (MMIO) Host features accepted GUEST */
348 uint32_t fDriverFeaturesWritten; /**< (MMIO) Host features complete tracking */
349 uint32_t uDeviceFeaturesSelect; /**< (MMIO) hi/lo select uDeviceFeatures GUEST */
350 uint32_t uDriverFeaturesSelect; /**< (MMIO) hi/lo select uDriverFeatures GUEST */
351 uint32_t uMsixConfig; /**< (MMIO) MSI-X vector GUEST */
352 uint8_t fDeviceStatus; /**< (MMIO) Device Status GUEST */
353 uint8_t fPrevDeviceStatus; /**< (MMIO) Prev Device Status GUEST */
354 uint8_t uConfigGeneration; /**< (MMIO) Device config sequencer HOST */
355 uint16_t uQueueNotify; /**< Caches queue idx in legacy mode GUEST */
356 bool fGenUpdatePending; /**< If set, update cfg gen after driver reads */
357 uint8_t uPciCfgDataOff; /**< Offset to PCI configuration data area */
358 uint8_t uISR; /**< Interrupt Status Register. */
359 uint8_t fMsiSupport; /**< Flag set if using MSI instead of ISR */
360 uint16_t uVirtqSelect; /**< (MMIO) queue selector GUEST */
361 uint32_t fLegacyDriver; /**< Set if guest drv < VirtIO 1.0 and allowed */
362 uint32_t fOfferLegacy; /**< Set at init call from dev-specific code */
363
364 /** @name The locations of the capability structures in PCI config space and the BAR.
365 * @{ */
366 VIRTIO_PCI_CAP_LOCATIONS_T LocPciCfgCap; /**< VIRTIO_PCI_CFG_CAP_T */
367 VIRTIO_PCI_CAP_LOCATIONS_T LocNotifyCap; /**< VIRTIO_PCI_NOTIFY_CAP_T */
368 VIRTIO_PCI_CAP_LOCATIONS_T LocCommonCfgCap; /**< VIRTIO_PCI_CAP_T */
369 VIRTIO_PCI_CAP_LOCATIONS_T LocIsrCap; /**< VIRTIO_PCI_CAP_T */
370 VIRTIO_PCI_CAP_LOCATIONS_T LocDeviceCap; /**< VIRTIO_PCI_CAP_T + custom data. */
371 /** @} */
372
373 IOMMMIOHANDLE hMmioPciCap; /**< MMIO handle of PCI cap. region (\#2) */
374 IOMIOPORTHANDLE hLegacyIoPorts; /**< Handle of legacy I/O port range. */
375
376#ifdef VBOX_WITH_STATISTICS
377 /** @name Statistics
378 * @{ */
379 STAMCOUNTER StatDescChainsAllocated;
380 STAMCOUNTER StatDescChainsFreed;
381 STAMCOUNTER StatDescChainsSegsIn;
382 STAMCOUNTER StatDescChainsSegsOut;
383 STAMPROFILEADV StatReadR3; /** I/O port and MMIO R3 Read profiling */
384 STAMPROFILEADV StatReadR0; /** I/O port and MMIO R0 Read profiling */
385 STAMPROFILEADV StatReadRC; /** I/O port and MMIO R3 Read profiling */
386 STAMPROFILEADV StatWriteR3; /** I/O port and MMIO R3 Write profiling */
387 STAMPROFILEADV StatWriteR0; /** I/O port and MMIO R3 Write profiling */
388 STAMPROFILEADV StatWriteRC; /** I/O port and MMIO R3 Write profiling */
389#endif
390 /** @} */
391
392} VIRTIOCORE;
393
394#define MAX_NAME 64
395
396/**
397 * The core/common state of the VirtIO PCI devices, ring-3 edition.
398 */
399typedef struct VIRTIOCORER3
400{
401 /** @name Callbacks filled by the device before calling virtioCoreR3Init.
402 * @{ */
403 /**
404 * Implementation-specific client callback to report VirtIO when feature negotiation is
405 * complete. It should be invoked by the VirtIO core only once.
406 *
407 * @param pVirtio Pointer to the shared virtio state.
408 * @param fDriverFeatures Bitmask of features the guest driver has accepted/declined.
409 * @param fLegacy true if legacy mode offered and until guest driver identifies itself
410 * as modern(e.g. VirtIO 1.0 featured)
411 */
412 DECLCALLBACKMEMBER(void, pfnFeatureNegotiationComplete, (PVIRTIOCORE pVirtio, uint64_t fDriverFeatures, uint32_t fLegacy));
413
414 /**
415 * Implementation-specific client callback to notify client of significant device status
416 * changes.
417 *
418 * @param pVirtio Pointer to the shared virtio state.
419 * @param pVirtioCC Pointer to the ring-3 virtio state.
420 * @param fDriverOk True if guest driver is okay (thus queues, etc... are
421 * valid)
422 */
423 DECLCALLBACKMEMBER(void, pfnStatusChanged,(PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, uint32_t fDriverOk));
424
425 /**
426 * Implementation-specific client callback to access VirtIO Device-specific capabilities
427 * (other VirtIO capabilities and features are handled in VirtIO implementation)
428 *
429 * @param pDevIns The device instance.
430 * @param offCap Offset within device specific capabilities struct.
431 * @param pvBuf Buffer in which to save read data.
432 * @param cbToRead Number of bytes to read.
433 */
434 DECLCALLBACKMEMBER(int, pfnDevCapRead,(PPDMDEVINS pDevIns, uint32_t offCap, void *pvBuf, uint32_t cbToRead));
435
436 /**
437 * Implementation-specific client callback to access VirtIO Device-specific capabilities
438 * (other VirtIO capabilities and features are handled in VirtIO implementation)
439 *
440 * @param pDevIns The device instance.
441 * @param offCap Offset within device specific capabilities struct.
442 * @param pvBuf Buffer with the bytes to write.
443 * @param cbToWrite Number of bytes to write.
444 */
445 DECLCALLBACKMEMBER(int, pfnDevCapWrite,(PPDMDEVINS pDevIns, uint32_t offCap, const void *pvBuf, uint32_t cbWrite));
446
447 /**
448 * When guest-to-host queue notifications are enabled, the guest driver notifies the host
449 * that the avail queue has buffers, and this callback informs the client.
450 *
451 * @param pVirtio Pointer to the shared virtio state.
452 * @param pVirtioCC Pointer to the ring-3 virtio state.
453 * @param uVirtqNbr Index of the notified queue
454 */
455 DECLCALLBACKMEMBER(void, pfnVirtqNotified,(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr));
456
457 /** @} */
458
459 R3PTRTYPE(PVIRTIO_PCI_CFG_CAP_T) pPciCfgCap; /**< Pointer to struct in PCI config area. */
460 R3PTRTYPE(PVIRTIO_PCI_NOTIFY_CAP_T) pNotifyCap; /**< Pointer to struct in PCI config area. */
461 R3PTRTYPE(PVIRTIO_PCI_CAP_T) pCommonCfgCap; /**< Pointer to struct in PCI config area. */
462 R3PTRTYPE(PVIRTIO_PCI_CAP_T) pIsrCap; /**< Pointer to struct in PCI config area. */
463 R3PTRTYPE(PVIRTIO_PCI_CAP_T) pDeviceCap; /**< Pointer to struct in PCI config area. */
464
465 uint32_t cbDevSpecificCfg; /**< Size of client's dev-specific config data */
466 R3PTRTYPE(uint8_t *) pbDevSpecificCfg; /**< Pointer to client's struct */
467 R3PTRTYPE(uint8_t *) pbPrevDevSpecificCfg; /**< Previous read dev-specific cfg of client */
468 bool fGenUpdatePending; /**< If set, update cfg gen after driver reads */
469 char szMmioName[MAX_NAME]; /**< MMIO mapping name */
470 char szPortIoName[MAX_NAME]; /**< PORT mapping name */
471} VIRTIOCORER3;
472
473/**
474 * The core/common state of the VirtIO PCI devices, ring-0 edition.
475 */
476typedef struct VIRTIOCORER0
477{
478 /**
479 * This callback notifies the device-specific portion of this device implementation (if guest-to-host
480 * queue notifications are enabled), that the guest driver has notified the host (this device)
481 * that the VirtIO "avail" ring of a queue has some new s/g buffers added by the guest VirtIO driver.
482 *
483 * @param pVirtio Pointer to the shared virtio state.
484 * @param pVirtioCC Pointer to the ring-3 virtio state.
485 * @param uVirtqNbr Index of the notified queue
486 */
487 DECLCALLBACKMEMBER(void, pfnVirtqNotified,(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr));
488
489} VIRTIOCORER0;
490
491/**
492 * The core/common state of the VirtIO PCI devices, raw-mode edition.
493 */
494typedef struct VIRTIOCORERC
495{
496 uint64_t uUnusedAtTheMoment;
497} VIRTIOCORERC;
498
499/** @typedef VIRTIOCORECC
500 * The instance data for the current context. */
501typedef CTX_SUFF(VIRTIOCORE) VIRTIOCORECC;
502
503/** @name API for VirtIO parent device
504 * @{ */
505
506/**
507 * Setup PCI device controller and Virtio state
508 *
509 * This should be called from PDMDEVREGR3::pfnConstruct.
510 *
511 * @param pDevIns Device instance.
512 * @param pVirtio Pointer to the shared virtio state. This
513 * must be the first member in the shared
514 * device instance data!
515 * @param pVirtioCC Pointer to the ring-3 virtio state. This
516 * must be the first member in the ring-3
517 * device instance data!
518 * @param pPciParams Values to populate industry standard PCI Configuration Space data structure
519 * @param pcszInstance Device instance name (format-specifier)
520 * @param fDevSpecificFeatures VirtIO device-specific features offered by
521 * client
522 * @param cbDevSpecificCfg Size of virtio_pci_device_cap device-specific struct
523 * @param pvDevSpecificCfg Address of client's dev-specific
524 * configuration struct.
525 */
526int virtioCoreR3Init(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC,
527 PVIRTIOPCIPARAMS pPciParams, const char *pcszInstance,
528 uint64_t fDevSpecificFeatures, uint32_t fOfferLegacy, void *pvDevSpecificCfg, uint16_t cbDevSpecificCfg);
529/**
530 * Initiate orderly reset procedure. This is an exposed API for clients that might need it.
531 * Invoked by client to reset the device and driver (see VirtIO 1.0 section 2.1.1/2.1.2)
532 *
533 * @param pVirtio Pointer to the virtio state.
534 */
535void virtioCoreResetAll(PVIRTIOCORE pVirtio);
536
537/**
538 * 'Attaches' host device-specific implementation's queue state to host VirtIO core
539 * virtqueue management infrastructure, informing the virtio core of the name of the
540 * queue to associate with the queue number.
541
542 * Note: uVirtqNbr (ordinal index) is used as the 'handle' for virtqs in this VirtioCore
543 * implementation's API (as an opaque selector into the VirtIO core's array of queues' states).
544 *
545 * Virtqueue numbers are actually VirtIO-specification defined device-specifically
546 * (i.e. they are unique within each VirtIO device type), but are in some cases scalable
547 * so only the pattern of queue numbers is defined by the spec and implementations may contain
548 * a self-determined plurality of queues.
549 *
550 * @param pVirtio Pointer to the shared virtio state.
551 * @param uVirtqNbr Virtq number
552 * @param pcszName Name to give queue
553 *
554 * @returns VBox status code.
555 */
556int virtioCoreR3VirtqAttach(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr, const char *pcszName);
557
558/**
559 * Detaches host device-specific implementation's queue state from the host VirtIO core
560 * virtqueue management infrastructure, informing the VirtIO core that the queue is
561 * not utilized by the device-specific code.
562 *
563 * @param pVirtio Pointer to the shared virtio state.
564 * @param uVirtqNbr Virtq number
565 * @param pcszName Name to give queue
566 *
567 * @returns VBox status code.
568 */
569int virtioCoreR3VirtqDetach(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
570
571/**
572 * Checks to see whether queue is attached to core.
573 *
574 * @param pVirtio Pointer to the shared virtio state.
575 * @param uVirtqNbr Virtq number
576 *
577 * Returns boolean true or false indicating whether dev-specific reflection
578 * of queue is attached to core.
579 */
580bool virtioCoreR3VirtqIsAttached(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
581
582/**
583 * Checks to see whether queue is enabled.
584 *
585 * @param pVirtio Pointer to the shared virtio state.
586 * @param uVirtqNbr Virtq number
587 *
588 * Returns boolean true or false indicating core queue enable state.
589 * There is no API function to enable the queue, because the actual enabling is handled
590 * by the guest via MMIO.
591 *
592 * NOTE: Guest VirtIO driver's claim over this state is overridden (which violates VirtIO 1.0 spec
593 * in a carefully controlled manner) in the case where the queue MUST be disabled, due to observed
594 * control queue corruption (e.g. null GCPhys virtq base addr) while restoring legacy-only device's
595 * (DevVirtioNet.cpp) as a way to flag that the queue is unusable-as-saved and must to be removed.
596 * That is all handled in the load/save exec logic. Device reset could potentially, depending on
597 * parameters passed from host VirtIO device to guest VirtIO driver, result in guest re-establishing
598 * queue, except, in that situation, the queue operational state would be valid.
599 */
600bool virtioCoreR3VirtqIsEnabled(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
601
602/**
603 * Enable or disable notification for the specified queue.
604 *
605 * When queue notifications are enabled, the guest VirtIO driver notifies host VirtIO device
606 * (via MMIO, see VirtIO 1.0, 4.1.4.4 "Notification Structure Layout") whenever guest driver adds
607 * a new s/g buffer to the "avail" ring of the queue.
608 *
609 * Note: VirtIO queue layout includes flags the device controls in "used" ring to inform guest
610 * driver if it should notify host of guest's buffer additions to the "avail" ring, and
611 * conversely, the guest driver sets flags in the "avail" ring to communicate to host device
612 * whether or not to interrupt guest when it adds buffers to used ring.
613 *
614 * @param pVirtio Pointer to the shared virtio state.
615 * @param uVirtqNbr Virtq number
616 * @param fEnable Selects notification mode (enabled or disabled)
617 */
618void virtioCoreVirtqEnableNotify(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr, bool fEnable);
619
620/**
621 * Notifies guest (via ISR or MSI-X) of device configuration change
622 *
623 * @param pVirtio Pointer to the shared virtio state.
624 */
625void virtioCoreNotifyConfigChanged(PVIRTIOCORE pVirtio);
626
627/**
628 * Displays a well-formatted human-readable translation of otherwise inscrutable bitmasks
629 * that embody features VirtIO specification definitions, indicating: Totality of features
630 * that can be implemented by host and guest, which features were offered by the host, and
631 * which were actually accepted by the guest. It displays it as a summary view of the device's
632 * finalized operational state (host-guest negotiated architecture) in such a way that shows
633 * which options are available for implementing or enabling.
634 *
635 * The non-device-specific VirtIO features list are managed by core API (e.g. implied).
636 * Only dev-specific features must be passed as parameter.
637
638 * @param pVirtio Pointer to the shared virtio state.
639 * @param pHlp Pointer to the debug info hlp struct
640 * @param s_aDevSpecificFeatures Dev-specific features (virtio-net, virtio-scsi...)
641 * @param cFeatures Number of features in aDevSpecificFeatures
642 */
643void virtioCorePrintDeviceFeatures(VIRTIOCORE *pVirtio, PCDBGFINFOHLP pHlp,
644 const VIRTIO_FEATURES_LIST *aDevSpecificFeatures, int cFeatures);
645
646/*
647 * Debug-assist utility function to display state of the VirtIO core code, including
648 * an overview of the state of all of the queues.
649 *
650 * This can be invoked when running the VirtualBox debugger, or from the command line
651 * using the command: "VboxManage debugvm <VM name or id> info <device name> [args]"
652 *
653 * Example: VBoxManage debugvm myVnetVm info "virtio-net" help
654 *
655 * This is implemented currently to be invoked by the inheriting device-specific code
656 * (see the the VirtualBox virtio-net (VirtIO network controller device implementation)
657 * for an example of code that receive debugvm callback directly).
658 *
659 * DevVirtioNet lists available sub-options if no arguments are provided. In that
660 * example this virtq info related function is invoked hierarchically when virtio-net
661 * displays its device-specific queue info.
662 *
663 * @param pDevIns The device instance.
664 * @param pHlp Pointer to the debug info hlp struct
665 * @param pszArgs Arguments to function
666 */
667void virtioCoreR3VirtqInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs, int uVirtqNbr);
668
669/**
670 * Returns the number of avail bufs in the virtq.
671 *
672 * @param pDevIns The device instance.
673 * @param pVirtio Pointer to the shared virtio state.
674 * @param uVirtqNbr Virtqueue to return the count of buffers available for.
675 */
676uint16_t virtioCoreVirtqAvailBufCount(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
677
678/**
679 * This function is identical to virtioCoreR3VirtqAvailBufGet(), *except* it doesn't consume
680 * peeked buffer from avail ring of the virtq. The function *becomes* identical to the
681 * virtioCoreR3VirtqAvailBufGet() only if virtioCoreR3VirtqAvailRingNext() is invoked to
682 * consume buf from the queue's avail ring, followed by invocation of virtioCoreR3VirtqUsedBufPut(),
683 * to hand host-processed buffer back to guest, which completes guest-initiated virtq buffer circuit.
684 *
685 * @param pDevIns The device instance.
686 * @param pVirtio Pointer to the shared virtio state.
687 * @param uVirtqNbr Virtq number
688 * @param ppVirtqBuf Address to store pointer to descriptor chain that contains the
689 * pre-processed transaction information pulled from the virtq.
690 *
691 * @returns VBox status code:
692 * @retval VINF_SUCCESS Success
693 * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
694 * @retval VERR_NOT_AVAILABLE If the queue is empty.
695 */
696int virtioCoreR3VirtqAvailBufPeek(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr,
697 PPVIRTQBUF ppVirtqBuf);
698
699/**
700 * This function fetches the next buffer (descriptor chain) from the VirtIO "avail" ring of
701 * indicated queue, separating the buf's s/g vectors into OUT (e.g. guest-to-host)
702 * components and and IN (host-to-guest) components.
703 *
704 * Caller is responsible for GCPhys to host virtual memory conversions. If the
705 * virtq buffer being peeked at is "consumed", virtioCoreR3VirtqAvailRingNext() must
706 * be called, and after that virtioCoreR3VirtqUsedBufPut() must be called to
707 * complete the buffer transfer cycle with the guest.
708 *
709 * @param pDevIns The device instance.
710 * @param pVirtio Pointer to the shared virtio state.
711 * @param uVirtqNbr Virtq number
712 * @param ppVirtqBuf Address to store pointer to descriptor chain that contains the
713 * pre-processed transaction information pulled from the virtq.
714 * Returned reference must be released by calling
715 * virtioCoreR3VirtqBufRelease().
716 * @param fRemove flags whether to remove desc chain from queue (false = peek)
717 *
718 * @returns VBox status code:
719 * @retval VINF_SUCCESS Success
720 * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
721 * @retval VERR_NOT_AVAILABLE If the queue is empty.
722 */
723int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr,
724 PPVIRTQBUF ppVirtqBuf, bool fRemove);
725
726/**
727 * Fetches a specific descriptor chain using avail ring of indicated queue and converts the
728 * descriptor chain into its OUT (to device) and IN (to guest) components.
729 *
730 * The caller is responsible for GCPhys to host virtual memory conversions and *must*
731 * return the virtq buffer using virtioCoreR3VirtqUsedBufPut() to complete the roundtrip
732 * virtq transaction.
733 * *
734 * @param pDevIns The device instance.
735 * @param pVirtio Pointer to the shared virtio state.
736 * @param uVirtqNbr Virtq number
737 * @param ppVirtqBuf Address to store pointer to descriptor chain that contains the
738 * pre-processed transaction information pulled from the virtq.
739 * Returned reference must be released by calling
740 * virtioCoreR3VirtqBufRelease().
741 * @param fRemove flags whether to remove desc chain from queue (false = peek)
742 *
743 * @returns VBox status code:
744 * @retval VINF_SUCCESS Success
745 * @retval VERR_INVALID_STATE VirtIO not in ready state (asserted).
746 * @retval VERR_NOT_AVAILABLE If the queue is empty.
747 */
748int virtioCoreR3VirtqAvailBufGet(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr,
749 uint16_t uHeadIdx, PPVIRTQBUF ppVirtqBuf);
750
751/**
752 * Returns data to the guest to complete a transaction initiated by virtioCoreR3VirtqAvailBufGet(),
753 * (or virtioCoreR3VirtqAvailBufPeek()/virtioCoreR3VirtqBufSync() call pair), to complete each
754 * buffer transfer transaction (guest-host buffer cycle), ultimately moving each descriptor chain
755 * from the avail ring of a queue onto the used ring of the queue. Note that VirtIO buffer
756 * transactions are *always* initiated by the guest and completed by the host. In other words,
757 * for the host to send any I/O related data to the guest (and in some cases configuration data),
758 * the guest must provide buffers via the virtq's avail ring, for the host to fill.
759 *
760 * At some some point virtioCoreR3VirtqUsedRingSync() must be called to return data to the guest,
761 * completing all pending virtioCoreR3VirtqAvailBufPut() operations that have accumulated since
762 * the last call to virtioCoreR3VirtqUsedRingSync().
763
764 * @note This function effectively performs write-ahead to the used ring of the virtq.
765 * Data written won't be seen by the guest until the next call to virtioCoreVirtqUsedRingSync()
766 *
767 * @param pDevIns The device instance (for reading).
768 * @param pVirtio Pointer to the shared virtio state.
769 * @param uVirtqNbr Virtq number
770 *
771 * @param pSgVirtReturn Points to scatter-gather buffer of virtual memory
772 * segments the caller is returning to the guest.
773 *
774 * @param pVirtqBuf This contains the context of the scatter-gather
775 * buffer originally pulled from the queue.
776 *
777 * @param fFence If true (default), put up copy-fence (memory barrier) after
778 * copying to guest phys. mem.
779 *
780 * @returns VBox status code.
781 * @retval VINF_SUCCESS Success
782 * @retval VERR_INVALID_STATE VirtIO not in ready state
783 * @retval VERR_NOT_AVAILABLE Virtq is empty
784 *
785 * @note This function will not release any reference to pVirtqBuf. The
786 * caller must take care of that.
787 */
788int virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr, PRTSGBUF pSgVirtReturn,
789 PVIRTQBUF pVirtqBuf, bool fFence = true);
790
791
792/**
793 * Quicker variant of same-named function (directly above) that it overloads,
794 * Instead, this variant accepts as input a pointer to a buffer and count,
795 * instead of S/G buffer thus doesn't have to copy between two S/G buffers and avoids some overhead.
796 *
797 * @param pDevIns The device instance (for reading).
798 * @param pVirtio Pointer to the shared virtio state.
799 * @param uVirtqNbr Virtq number
800 * @param cb Number of bytes to add to copy to phys. buf.
801 * @param pv Virtual mem buf to copy to phys buf.
802 * @param cbEnqueue How many bytes in packet to enqueue (0 = don't enqueue)
803 * @param fFence If true (default), put up copy-fence (memory barrier) after
804 * copying to guest phys. mem.
805 *
806 * @returns VBox status code.
807 * @retval VINF_SUCCESS Success
808 * @retval VERR_INVALID_STATE VirtIO not in ready state
809 * @retval VERR_NOT_AVAILABLE Virtq is empty
810 *
811 * @note This function will not release any reference to pVirtqBuf. The
812 * caller must take care of that.
813 */
814int virtioCoreR3VirtqUsedBufPut(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtq, size_t cb, const void *pv,
815 PVIRTQBUF pVirtqBuf, size_t cbEnqueue, bool fFence = true);
816
817
818/**
819 * Advance index of avail ring to next entry in specified virtq (see virtioCoreR3VirtqAvailBufPeek())
820 *
821 * @param pVirtio Pointer to the virtio state.
822 * @param uVirtqNbr Index of queue
823 */
824int virtioCoreR3VirtqAvailBufNext(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
825
826/**
827 * Checks to see if guest has accepted host device's VIRTIO_F_VERSION_1 (i.e. "modern")
828 * behavioral modeling, indicating guest agreed to comply with the modern VirtIO 1.0+ specification.
829 * Otherwise unavoidable presumption is that the host device is dealing with legacy VirtIO
830 * guest driver, thus must be prepared to cope with less mature architecture and behaviors
831 * from prototype era of VirtIO. (see comments in PDM-invoked device constructor for more
832 * information).
833 *
834 * @param pVirtio Pointer to the virtio state.
835 */
836int virtioCoreIsLegacyMode(PVIRTIOCORE pVirtio);
837
838/**
839 * This VirtIO transitional device supports "modern" (rev 1.0+) as well as "legacy" (e.g. < 1.0) VirtIO drivers.
840 * Some legacy guest drivers are known to mishandle PCI bus mastering wherein the PCI flavor of GC phys
841 * access functions can't be used. The following wrappers select the memory access method based on whether the
842 * device is operating in legacy mode or not.
843 */
844DECLINLINE(int) virtioCoreGCPhysWrite(PVIRTIOCORE pVirtio, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, void *pvBuf, size_t cbWrite)
845{
846 int rc;
847 if (virtioCoreIsLegacyMode(pVirtio))
848 rc = PDMDevHlpPhysWrite(pDevIns, GCPhys, pvBuf, cbWrite);
849 else
850 rc = PDMDevHlpPCIPhysWrite(pDevIns, GCPhys, pvBuf, cbWrite);
851 return rc;
852}
853
854DECLINLINE(int) virtioCoreGCPhysRead(PVIRTIOCORE pVirtio, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead)
855{
856 int rc;
857 if (virtioCoreIsLegacyMode(pVirtio))
858 rc = PDMDevHlpPhysRead(pDevIns, GCPhys, pvBuf, cbRead);
859 else
860 rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhys, pvBuf, cbRead);
861 return rc;
862}
863
864/*
865 * (See comments for corresponding function in sg.h)
866 */
867DECLINLINE(void) virtioCoreGCPhysChainInit(PVIRTIOSGBUF pGcSgBuf, PVIRTIOSGSEG paSegs, size_t cSegs)
868{
869 AssertPtr(pGcSgBuf);
870 Assert((cSegs > 0 && RT_VALID_PTR(paSegs)) || (!cSegs && !paSegs));
871 Assert(cSegs < (~(unsigned)0 >> 1));
872
873 pGcSgBuf->paSegs = paSegs;
874 pGcSgBuf->cSegs = (unsigned)cSegs;
875 pGcSgBuf->idxSeg = 0;
876 if (cSegs && paSegs)
877 {
878 pGcSgBuf->GCPhysCur = paSegs[0].GCPhys;
879 pGcSgBuf->cbSegLeft = paSegs[0].cbSeg;
880 }
881 else
882 {
883 pGcSgBuf->GCPhysCur = 0;
884 pGcSgBuf->cbSegLeft = 0;
885 }
886}
887
888/*
889 * (See comments for corresponding function in sg.h)
890 */
891DECLINLINE(RTGCPHYS) virtioCoreGCPhysChainGet(PVIRTIOSGBUF pGcSgBuf, size_t *pcbData)
892{
893 size_t cbData;
894 RTGCPHYS pGcBuf;
895
896 /* Check that the S/G buffer has memory left. */
897 if (RT_LIKELY(pGcSgBuf->idxSeg < pGcSgBuf->cSegs && pGcSgBuf->cbSegLeft))
898 { /* likely */ }
899 else
900 {
901 *pcbData = 0;
902 return 0;
903 }
904
905 AssertMsg( pGcSgBuf->cbSegLeft <= 128 * _1M
906 && (RTGCPHYS)pGcSgBuf->GCPhysCur >= (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys
907 && (RTGCPHYS)pGcSgBuf->GCPhysCur + pGcSgBuf->cbSegLeft <=
908 (RTGCPHYS)pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys + pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg,
909 ("pGcSgBuf->idxSeg=%d pGcSgBuf->cSegs=%d pGcSgBuf->GCPhysCur=%p pGcSgBuf->cbSegLeft=%zd "
910 "pGcSgBuf->paSegs[%d].GCPhys=%p pGcSgBuf->paSegs[%d].cbSeg=%zd\n",
911 pGcSgBuf->idxSeg, pGcSgBuf->cSegs, pGcSgBuf->GCPhysCur, pGcSgBuf->cbSegLeft,
912 pGcSgBuf->idxSeg, pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys, pGcSgBuf->idxSeg,
913 pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg));
914
915 cbData = RT_MIN(*pcbData, pGcSgBuf->cbSegLeft);
916 pGcBuf = pGcSgBuf->GCPhysCur;
917 pGcSgBuf->cbSegLeft -= cbData;
918 if (!pGcSgBuf->cbSegLeft)
919 {
920 pGcSgBuf->idxSeg++;
921
922 if (pGcSgBuf->idxSeg < pGcSgBuf->cSegs)
923 {
924 pGcSgBuf->GCPhysCur = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].GCPhys;
925 pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[pGcSgBuf->idxSeg].cbSeg;
926 }
927 *pcbData = cbData;
928 }
929 else
930 pGcSgBuf->GCPhysCur = pGcSgBuf->GCPhysCur + cbData;
931
932 return pGcBuf;
933}
934
935/*
936 * (See comments for corresponding function in sg.h)
937 */
938DECLINLINE(void) virtioCoreGCPhysChainReset(PVIRTIOSGBUF pGcSgBuf)
939{
940 AssertPtrReturnVoid(pGcSgBuf);
941
942 pGcSgBuf->idxSeg = 0;
943 if (pGcSgBuf->cSegs)
944 {
945 pGcSgBuf->GCPhysCur = pGcSgBuf->paSegs[0].GCPhys;
946 pGcSgBuf->cbSegLeft = pGcSgBuf->paSegs[0].cbSeg;
947 }
948 else
949 {
950 pGcSgBuf->GCPhysCur = 0;
951 pGcSgBuf->cbSegLeft = 0;
952 }
953}
954
955/*
956 * (See comments for corresponding function in sg.h)
957 */
958DECLINLINE(RTGCPHYS) virtioCoreGCPhysChainAdvance(PVIRTIOSGBUF pGcSgBuf, size_t cbAdvance)
959{
960 AssertReturn(pGcSgBuf, 0);
961
962 size_t cbLeft = cbAdvance;
963 while (cbLeft)
964 {
965 size_t cbThisAdvance = cbLeft;
966 virtioCoreGCPhysChainGet(pGcSgBuf, &cbThisAdvance);
967 if (!cbThisAdvance)
968 break;
969
970 cbLeft -= cbThisAdvance;
971 }
972 return cbAdvance - cbLeft;
973}
974
975/*
976 * (See comments for corresponding function in sg.h)
977 */
978DECLINLINE(RTGCPHYS) virtioCoreGCPhysChainGetNextSeg(PVIRTIOSGBUF pGcSgBuf, size_t *pcbSeg)
979{
980 AssertReturn(pGcSgBuf, 0);
981 AssertPtrReturn(pcbSeg, 0);
982
983 if (!*pcbSeg)
984 *pcbSeg = pGcSgBuf->cbSegLeft;
985
986 return virtioCoreGCPhysChainGet(pGcSgBuf, pcbSeg);
987}
988
989/**
990 * Calculate the length of a GCPhys s/g buffer by tallying the size of each segment.
991 *
992 * @param pGcSgBuf Guest Context (GCPhys) S/G buffer to calculate length of
993 */
994DECLINLINE(size_t) virtioCoreGCPhysChainCalcBufSize(PCVIRTIOSGBUF pGcSgBuf)
995{
996 size_t cb = 0;
997 unsigned i = pGcSgBuf->cSegs;
998 while (i-- > 0)
999 cb += pGcSgBuf->paSegs[i].cbSeg;
1000 return cb;
1001}
1002
1003/*
1004 * (See comments for corresponding function in sg.h)
1005 */
1006DECLINLINE(size_t) virtioCoreGCPhysChainCalcLengthLeft(PVIRTIOSGBUF pGcSgBuf)
1007{
1008 size_t cb = pGcSgBuf->cbSegLeft;
1009 unsigned i = pGcSgBuf->cSegs;
1010 while (i-- > pGcSgBuf->idxSeg + 1)
1011 cb += pGcSgBuf->paSegs[i].cbSeg;
1012 return cb;
1013}
1014#define VIRTQNAME(a_pVirtio, a_uVirtq) ((a_pVirtio)->aVirtqueues[(a_uVirtq)].szName)
1015
1016/**
1017 * Convert and append bytes from a virtual-memory simple buffer to VirtIO guest's
1018 * physical memory described by a buffer pulled form the avail ring of a virtq.
1019 *
1020 * @param pVirtio Pointer to the shared virtio state.
1021 * @param pVirtqBuf VirtIO buffer to fill
1022 * @param pv input: virtual memory buffer to receive bytes
1023 * @param cb number of bytes to add to the s/g buffer.
1024 */
1025DECLINLINE(void) virtioCoreR3VirqBufFill(PVIRTIOCORE pVirtio, PVIRTQBUF pVirtqBuf, void *pv, size_t cb)
1026{
1027 uint8_t *pvBuf = (uint8_t *)pv;
1028 size_t cbRemain = cb, cbTotal = 0;
1029 PVIRTIOSGBUF pSgPhysReturn = pVirtqBuf->pSgPhysReturn;
1030 while (cbRemain)
1031 {
1032 size_t cbBounded = RT_MIN(pSgPhysReturn->cbSegLeft, cbRemain);
1033 Assert(cbBounded > 0);
1034 virtioCoreGCPhysWrite(pVirtio, CTX_SUFF(pVirtio->pDevIns), (RTGCPHYS)pSgPhysReturn->GCPhysCur, pvBuf, cbBounded);
1035 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbBounded);
1036 pvBuf += cbBounded;
1037 cbRemain -= cbBounded;
1038 cbTotal += cbBounded;
1039 }
1040 LogFunc(("Appended %d bytes to guest phys buf [head: %u]. %d bytes unused in buf.)\n",
1041 cbTotal, pVirtqBuf->uHeadIdx, virtioCoreGCPhysChainCalcLengthLeft(pSgPhysReturn)));
1042}
1043
1044/**
1045 * Extract some bytes from of a virtq s/g buffer, converting them from GCPhys space to
1046 * to ordinary virtual memory (i.e. making data directly accessible to host device code)
1047 *
1048 * As a performance optimization, it is left to the caller to validate buffer size.
1049 *
1050 * @param pVirtio Pointer to the shared virtio state.
1051 * @param pVirtqBuf input: virtq buffer
1052 * @param pv output: virtual memory buffer to receive bytes
1053 * @param cb number of bytes to Drain from buffer
1054 */
1055DECLINLINE(void) virtioCoreR3VirtqBufDrain(PVIRTIOCORE pVirtio, PVIRTQBUF pVirtqBuf, void *pv, size_t cb)
1056{
1057 uint8_t *pb = (uint8_t *)pv;
1058 size_t cbLim = RT_MIN(pVirtqBuf->cbPhysSend, cb);
1059 while (cbLim)
1060 {
1061 size_t cbSeg = cbLim;
1062 RTGCPHYS GCPhys = virtioCoreGCPhysChainGetNextSeg(pVirtqBuf->pSgPhysSend, &cbSeg);
1063 PDMDevHlpPCIPhysRead(pVirtio->pDevInsR3, GCPhys, pb, cbSeg);
1064 pb += cbSeg;
1065 cbLim -= cbSeg;
1066 pVirtqBuf->cbPhysSend -= cbSeg;
1067 }
1068 LogFunc(("Drained %d/%d bytes from %s buffer, head idx: %u (%d bytes left)\n",
1069 cb - cbLim, cb, VIRTQNAME(pVirtio, pVirtqBuf->uVirtq),
1070 pVirtqBuf->uHeadIdx, virtioCoreGCPhysChainCalcLengthLeft(pVirtqBuf->pSgPhysReturn)));
1071}
1072
1073#undef VIRTQNAME
1074
1075/**
1076 * Updates indicated virtq's "used ring" descriptor index to match "shadow" index that tracks
1077 * pending buffers added to the used ring, thus exposing all the data added by virtioCoreR3VirtqUsedBufPut()
1078 * to the "used ring" since the last virtioCoreVirtqUsedRingSync().
1079 *
1080 * This *must* be invoked after one or more virtioCoreR3VirtqUsedBufPut() calls to inform guest driver
1081 * there is data in the queue. If enabled by guest, IRQ or MSI-X signalling will notify guest
1082 * proactively, otherwise guest detects updates by polling. (see VirtIO 1.0, Section 2.4 "Virtqueues").
1083 *
1084 * @param pDevIns The device instance.
1085 * @param pVirtio Pointer to the shared virtio state.
1086 * @param uVirtqNbr Virtq number
1087 *
1088 * @returns VBox status code.
1089 * @retval VINF_SUCCESS Success
1090 * @retval VERR_INVALID_STATE VirtIO not in ready state
1091 */
1092int virtioCoreVirtqUsedRingSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr);
1093
1094/**
1095 * Retains a reference to the given descriptor chain.
1096 *
1097 * @param pVirtqBuf The descriptor chain to reference.
1098 *
1099 * @returns New reference count.
1100 * @retval UINT32_MAX on invalid parameter.
1101 */
1102uint32_t virtioCoreR3VirtqBufRetain(PVIRTQBUF pVirtqBuf);
1103
1104/**
1105 * Releases a reference to the given descriptor chain.
1106 *
1107 * @param pVirtio Pointer to the shared virtio state.
1108 * @param pVirtqBuf The descriptor chain to reference. NULL is quietly
1109 * ignored (returns 0).
1110 * @returns New reference count.
1111 * @retval 0 if freed or invalid parameter.
1112 */
1113uint32_t virtioCoreR3VirtqBufRelease(PVIRTIOCORE pVirtio, PVIRTQBUF pVirtqBuf);
1114
1115/**
1116 * Return queue enable state
1117 *
1118 * @param pVirtio Pointer to the virtio state.
1119 * @param uVirtqNbr Virtq number.
1120 *
1121 * @returns true or false indicating queue is enabled or not.
1122 */
1123DECLINLINE(bool) virtioCoreIsVirtqEnabled(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
1124{
1125 Assert(uVirtqNbr < RT_ELEMENTS(pVirtio->aVirtqueues));
1126 if (pVirtio->fLegacyDriver)
1127 return pVirtio->aVirtqueues[uVirtqNbr].GCPhysVirtqDesc != 0;
1128 return pVirtio->aVirtqueues[uVirtqNbr].uEnable != 0;
1129}
1130
1131/**
1132 * Get name of queue, via uVirtqNbr, assigned during virtioCoreR3VirtqAttach()
1133 *
1134 * @param pVirtio Pointer to the virtio state.
1135 * @param uVirtqNbr Virtq number.
1136 *
1137 * @returns Pointer to read-only queue name.
1138 */
1139DECLINLINE(const char *) virtioCoreVirtqGetName(PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
1140{
1141 Assert((size_t)uVirtqNbr < RT_ELEMENTS(pVirtio->aVirtqueues));
1142 return pVirtio->aVirtqueues[uVirtqNbr].szName;
1143}
1144
1145/**
1146 * Get the bitmask of features VirtIO is running with. This is called by the device-specific
1147 * VirtIO implementation to identify this device's operational configuration after features
1148 * have been negotiated with guest VirtIO driver. Feature negotiation entails host indicating
1149 * to guest which features it supports, then guest accepting from among the offered, which features
1150 * it will enable. That becomes the agreement between the host and guest. The bitmask containing
1151 * virtio core features plus device-specific features is provided as a parameter to virtioCoreR3Init()
1152 * by the host side device-specific virtio implementation.
1153 *
1154 * @param pVirtio Pointer to the virtio state.
1155 *
1156 * @returns Features the guest driver has accepted, finalizing the operational features
1157 */
1158DECLINLINE(uint64_t) virtioCoreGetNegotiatedFeatures(PVIRTIOCORE pVirtio)
1159{
1160 return pVirtio->uDriverFeatures;
1161}
1162
1163/**
1164 * Get name of the VM state change associated with the enumeration variable
1165 *
1166 * @param enmState VM state (enumeration value)
1167 *
1168 * @returns associated text.
1169 */
1170const char *virtioCoreGetStateChangeText(VIRTIOVMSTATECHANGED enmState);
1171
1172/**
1173 * Debug assist code for any consumer that inherits VIRTIOCORE.
1174 * Log memory-mapped I/O input or output value.
1175 *
1176 * This is to be invoked by macros that assume they are invoked in functions with
1177 * the relevant arguments. (See Virtio_1_0.cpp).
1178 *
1179 * It is exposed via the API so inheriting device-specific clients can provide similar
1180 * logging capabilities for a consistent look-and-feel.
1181 *
1182 * @param pszFunc To avoid displaying this function's name via __FUNCTION__ or LogFunc()
1183 * @param pszMember Name of struct member
1184 * @param pv pointer to value
1185 * @param cb size of value
1186 * @param uOffset offset into member where value starts
1187 * @param fWrite True if write I/O
1188 * @param fHasIndex True if the member is indexed
1189 * @param idx The index if fHasIndex
1190 */
1191void virtioCoreLogMappedIoValue(const char *pszFunc, const char *pszMember, uint32_t uMemberSize,
1192 const void *pv, uint32_t cb, uint32_t uOffset,
1193 int fWrite, int fHasIndex, uint32_t idx);
1194
1195/**
1196 * Debug assist for any consumer
1197 *
1198 * Does a formatted hex dump using Log(()), recommend using VIRTIO_HEX_DUMP() macro to
1199 * control enabling of logging efficiently.
1200 *
1201 * @param pv pointer to buffer to dump contents of
1202 * @param cb count of characters to dump from buffer
1203 * @param uBase base address of per-row address prefixing of hex output
1204 * @param pszTitle Optional title. If present displays title that lists
1205 * provided text with value of cb to indicate VIRTQ_SIZE next to it.
1206 */
1207void virtioCoreHexDump(uint8_t *pv, uint32_t cb, uint32_t uBase, const char *pszTitle);
1208
1209/**
1210 * Debug assist for any consumer device code
1211 * Do a hex dump of memory in guest physical context
1212 *
1213 * @param GCPhys pointer to buffer to dump contents of
1214 * @param cb count of characters to dump from buffer
1215 * @param uBase base address of per-row address prefixing of hex output
1216 * @param pszTitle Optional title. If present displays title that lists
1217 * provided text with value of cb to indicate size next to it.
1218 */
1219void virtioCoreGCPhysHexDump(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, uint16_t cb, uint32_t uBase, const char *pszTitle);
1220
1221/**
1222 * The following API is functions identically to the similarly-named calls pertaining to the RTSGBUF
1223 */
1224
1225/** Misc VM and PDM boilerplate */
1226int virtioCoreR3SaveExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t cQueues);
1227int virtioCoreR3ModernDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uTestVersion, uint32_t cQueues);
1228int virtioCoreR3LegacyDeviceLoadExec(PVIRTIOCORE pVirtio, PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uVirtioLegacy_3_1_Beta);
1229void virtioCoreR3VmStateChanged(PVIRTIOCORE pVirtio, VIRTIOVMSTATECHANGED enmState);
1230void virtioCoreR3Term(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC);
1231int virtioCoreRZInit(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio);
1232const char *virtioCoreGetStateChangeText(VIRTIOVMSTATECHANGED enmState);
1233
1234/*
1235 * The following macros assist with handling/logging MMIO accesses to VirtIO dev-specific config area,
1236 * in a way that enhances code readability and debug logging consistency.
1237 *
1238 * cb, pv and fWrite are implicit parameters and must be defined by the invoker.
1239 */
1240#ifdef LOG_ENABLED
1241
1242# define VIRTIO_DEV_CONFIG_LOG_ACCESS(member, tCfgStruct, uOffsetOfAccess) \
1243 if (LogIs7Enabled()) { \
1244 uint32_t uMbrOffset = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1245 uint32_t uMbrSize = RT_SIZEOFMEMB(tCfgStruct, member); \
1246 virtioCoreLogMappedIoValue(__FUNCTION__, #member, uMbrSize, pv, cb, uMbrOffset, fWrite, false, 0); \
1247 }
1248
1249# define VIRTIO_DEV_CONFIG_LOG_INDEXED_ACCESS(member, tCfgStruct, uOffsetOfAccess, uIdx) \
1250 if (LogIs7Enabled()) { \
1251 uint32_t uMbrOffset = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1252 uint32_t uMbrSize = RT_SIZEOFMEMB(tCfgStruct, member); \
1253 virtioCoreLogMappedIoValue(__FUNCTION__, #member, uMbrSize, pv, cb, uMbrOffset, fWrite, true, uIdx); \
1254 }
1255#else
1256# define VIRTIO_DEV_CONFIG_LOG_ACCESS(member, tCfgStruct, uMbrOffset) do { } while (0)
1257# define VIRTIO_DEV_CONFIG_LOG_INDEXED_ACCESS(member, tCfgStruct, uMbrOffset, uIdx) do { } while (0)
1258#endif
1259
1260DECLINLINE(bool) virtioCoreMatchMember(uint32_t uOffset, uint32_t cb, uint32_t uMemberOff,
1261 size_t uMemberSize, bool fSubFieldMatch)
1262{
1263 /* Test for 8-byte field (always accessed as two 32-bit components) */
1264 if (uMemberSize == 8)
1265 return (cb == sizeof(uint32_t)) && (uOffset == uMemberOff || uOffset == (uMemberOff + sizeof(uint32_t)));
1266
1267 if (fSubFieldMatch)
1268 return (uOffset >= uMemberOff) && (cb <= uMemberSize - (uOffset - uMemberOff));
1269
1270 /* Test for exact match */
1271 return (uOffset == uMemberOff) && (cb == uMemberSize);
1272}
1273
1274/**
1275 * Yields boolean true if uOffsetOfAccess falls within bytes of specified member of config struct
1276 */
1277#define VIRTIO_DEV_CONFIG_SUBMATCH_MEMBER(member, tCfgStruct, uOffsetOfAccess) \
1278 virtioCoreMatchMember(uOffsetOfAccess, cb, \
1279 RT_UOFFSETOF(tCfgStruct, member), \
1280 RT_SIZEOFMEMB(tCfgStruct, member), true /* fSubfieldMatch */)
1281
1282#define VIRTIO_DEV_CONFIG_MATCH_MEMBER(member, tCfgStruct, uOffsetOfAccess) \
1283 virtioCoreMatchMember(uOffsetOfAccess, cb, \
1284 RT_UOFFSETOF(tCfgStruct, member), \
1285 RT_SIZEOFMEMB(tCfgStruct, member), false /* fSubfieldMatch */)
1286
1287
1288
1289/**
1290 * Copy reads or copy writes specified member field of config struct (based on fWrite),
1291 * the memory described by cb and pv.
1292 *
1293 * cb, pv and fWrite are implicit parameters and must be defined by invoker.
1294 */
1295#define VIRTIO_DEV_CONFIG_ACCESS(member, tCfgStruct, uOffsetOfAccess, pCfgStruct) \
1296 do \
1297 { \
1298 uint32_t uOffsetInMember = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1299 if (fWrite) \
1300 memcpy(((char *)&(pCfgStruct)->member) + uOffsetInMember, pv, cb); \
1301 else \
1302 memcpy(pv, ((const char *)&(pCfgStruct)->member) + uOffsetInMember, cb); \
1303 VIRTIO_DEV_CONFIG_LOG_ACCESS(member, tCfgStruct, uOffsetOfAccess); \
1304 } while(0)
1305
1306/**
1307 * Copies bytes into memory described by cb, pv from the specified member field of the config struct.
1308 * The operation is a NOP, logging an error if an implied parameter, fWrite, is boolean true.
1309 *
1310 * cb, pv and fWrite are implicit parameters and must be defined by the invoker.
1311 */
1312#define VIRTIO_DEV_CONFIG_ACCESS_READONLY(member, tCfgStruct, uOffsetOfAccess, pCfgStruct) \
1313 do \
1314 { \
1315 uint32_t uOffsetInMember = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1316 if (fWrite) \
1317 LogFunc(("Guest attempted to write readonly virtio config struct (member %s)\n", #member)); \
1318 else \
1319 { \
1320 memcpy(pv, ((const char *)&(pCfgStruct)->member) + uOffsetInMember, cb); \
1321 VIRTIO_DEV_CONFIG_LOG_ACCESS(member, tCfgStruct, uOffsetOfAccess); \
1322 } \
1323 } while(0)
1324
1325/**
1326 * Copies into or out of specified member field of config struct (based on fWrite),
1327 * the memory described by cb and pv.
1328 *
1329 * cb, pv and fWrite are implicit parameters and must be defined by invoker.
1330 */
1331#define VIRTIO_DEV_CONFIG_ACCESS_INDEXED(member, uIdx, tCfgStruct, uOffsetOfAccess, pCfgStruct) \
1332 do \
1333 { \
1334 uint32_t uOffsetInMember = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1335 if (fWrite) \
1336 memcpy(((char *)&(pCfgStruct[uIdx].member)) + uOffsetInMember, pv, cb); \
1337 else \
1338 memcpy(pv, ((const char *)&(pCfgStruct[uIdx].member)) + uOffsetInMember, cb); \
1339 VIRTIO_DEV_CONFIG_LOG_INDEXED_ACCESS(member, tCfgStruct, uOffsetOfAccess, uIdx); \
1340 } while(0)
1341
1342/**
1343 * Copies bytes into memory described by cb, pv from the specified member field of the config struct.
1344 * The operation is a nop and logs error if implied parameter fWrite is true.
1345 *
1346 * cb, pv and fWrite are implicit parameters and must be defined by invoker.
1347 */
1348#define VIRTIO_DEV_CONFIG_ACCESS_INDEXED_READONLY(member, uidx, tCfgStruct, uOffsetOfAccess, pCfgStruct) \
1349 do \
1350 { \
1351 uint32_t uOffsetInMember = uOffsetOfAccess - RT_UOFFSETOF(tCfgStruct, member); \
1352 if (fWrite) \
1353 LogFunc(("Guest attempted to write readonly virtio config struct (member %s)\n", #member)); \
1354 else \
1355 { \
1356 memcpy(pv, ((const char *)&(pCfgStruct[uIdx].member)) + uOffsetInMember, cb); \
1357 VIRTIO_DEV_CONFIG_LOG_INDEXED_ACCESS(member, tCfgStruct, uOffsetOfAccess, uIdx); \
1358 } \
1359 } while(0)
1360
1361/** @} */
1362
1363/** @name API for VirtIO parent device
1364 * @{ */
1365
1366#endif /* !VBOX_INCLUDED_SRC_VirtIO_VirtioCore_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette