VirtualBox

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

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

Fix error handling bug and a few typos

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

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