VirtualBox

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

Last change on this file since 97046 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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