VirtualBox

source: vbox/trunk/src/VBox/Devices/VirtIO/Virtio.h@ 91396

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

Dev*: Checked up all the PDMDevHlpCritSectEnter calls to make sure the status code is checked. bugref:6695

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: Virtio.h 90447 2021-07-31 00:44:13Z vboxsync $ */
2/** @file
3 * Virtio.h - Virtio Declarations
4 */
5
6/*
7 * Copyright (C) 2009-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_INCLUDED_SRC_VirtIO_Virtio_h
19#define VBOX_INCLUDED_SRC_VirtIO_Virtio_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#include <iprt/types.h>
25
26
27/** Pointer to the core shared state of a VirtIO PCI device */
28typedef struct VPCISTATE *PVPCISTATE;
29/** Pointer to the core ring-3 state of a VirtIO PCI device */
30typedef struct VPCISTATER3 *PVPCISTATER3;
31/** Pointer to the core ring-0 state of a VirtIO PCI device */
32typedef struct VPCISTATER0 *PVPCISTATER0;
33/** Pointer to the core raw-mode state of a VirtIO PCI device */
34typedef struct VPCISTATERC *PVPCISTATERC;
35
36/** Pointer to the core current context state of a VirtIO PCI device */
37typedef CTX_SUFF(PVPCISTATE) PVPCISTATECC;
38/** The core current context state of a VirtIO PCI device */
39typedef struct CTX_SUFF(VPCISTATE) VPCISTATECC;
40
41
42/** @name Saved state versions.
43 * The saved state version is changed if either common or any of specific
44 * parts are changed. That is, it is perfectly possible that the version
45 * of saved vnet state will increase as a result of change in vblk structure
46 * for example.
47 */
48#define VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1 1
49#define VIRTIO_SAVEDSTATE_VERSION 2
50/** @} */
51
52#define DEVICE_PCI_VENDOR_ID 0x1AF4
53#define DEVICE_PCI_BASE_ID 0x1000
54#define DEVICE_PCI_SUBSYSTEM_VENDOR_ID 0x1AF4
55#define DEVICE_PCI_SUBSYSTEM_BASE_ID 1
56
57#define VIRTIO_MAX_NQUEUES 3
58
59#define VPCI_HOST_FEATURES 0x0
60#define VPCI_GUEST_FEATURES 0x4
61#define VPCI_QUEUE_PFN 0x8
62#define VPCI_QUEUE_NUM 0xC
63#define VPCI_QUEUE_SEL 0xE
64#define VPCI_QUEUE_NOTIFY 0x10
65#define VPCI_STATUS 0x12
66#define VPCI_ISR 0x13
67#define VPCI_CONFIG 0x14
68
69#define VPCI_ISR_QUEUE 0x1
70#define VPCI_ISR_CONFIG 0x3
71
72#define VPCI_STATUS_ACK 0x01
73#define VPCI_STATUS_DRV 0x02
74#define VPCI_STATUS_DRV_OK 0x04
75#define VPCI_STATUS_FAILED 0x80
76
77#define VPCI_F_NOTIFY_ON_EMPTY UINT32_C(0x01000000)
78#define VPCI_F_ANY_LAYOUT UINT32_C(0x08000000)
79#define VPCI_F_RING_INDIRECT_DESC UINT32_C(0x10000000)
80#define VPCI_F_RING_EVENT_IDX UINT32_C(0x20000000)
81#define VPCI_F_BAD_FEATURE UINT32_C(0x40000000)
82
83#define VRINGDESC_MAX_SIZE (2 * 1024 * 1024)
84#define VRINGDESC_F_NEXT 0x01
85#define VRINGDESC_F_WRITE 0x02
86#define VRINGDESC_F_INDIRECT 0x04
87
88typedef struct VRINGDESC
89{
90 uint64_t u64Addr;
91 uint32_t uLen;
92 uint16_t u16Flags;
93 uint16_t u16Next;
94} VRINGDESC;
95typedef VRINGDESC *PVRINGDESC;
96
97#define VRINGAVAIL_F_NO_INTERRUPT 0x01
98
99typedef struct VRINGAVAIL
100{
101 uint16_t uFlags;
102 uint16_t uNextFreeIndex;
103 uint16_t auRing[1];
104} VRINGAVAIL;
105
106typedef struct VRINGUSEDELEM
107{
108 uint32_t uId;
109 uint32_t uLen;
110} VRINGUSEDELEM;
111
112#define VRINGUSED_F_NO_NOTIFY 0x01
113
114typedef struct VRingUsed
115{
116 uint16_t uFlags;
117 uint16_t uIndex;
118 VRINGUSEDELEM aRing[1];
119} VRINGUSED;
120typedef VRINGUSED *PVRINGUSED;
121
122#define VRING_MAX_SIZE 1024
123
124typedef struct VRING
125{
126 uint16_t uSize;
127 uint16_t padding[3];
128 RTGCPHYS addrDescriptors;
129 RTGCPHYS addrAvail;
130 RTGCPHYS addrUsed;
131} VRING;
132typedef VRING *PVRING;
133
134typedef struct VQUEUE
135{
136 VRING VRing;
137 uint16_t uNextAvailIndex;
138 uint16_t uNextUsedIndex;
139 uint32_t uPageNumber;
140 char szName[16];
141} VQUEUE;
142typedef VQUEUE *PVQUEUE;
143
144/**
145 * Queue callback (consumer?).
146 *
147 * @param pDevIns The device instance.
148 * @param pQueue Pointer to the queue structure.
149 */
150typedef DECLCALLBACKTYPE(void, FNVPCIQUEUECALLBACK,(PPDMDEVINS pDevIns, PVQUEUE pQueue));
151/** Pointer to a VQUEUE callback function. */
152typedef FNVPCIQUEUECALLBACK *PFNVPCIQUEUECALLBACK;
153
154typedef struct VQUEUER3
155{
156 R3PTRTYPE(PFNVPCIQUEUECALLBACK) pfnCallback;
157} VQUEUER3;
158typedef VQUEUER3 *PVQUEUER3;
159
160typedef struct VQUEUESEG
161{
162 RTGCPHYS addr;
163 void *pv;
164 uint32_t cb;
165} VQUEUESEG;
166
167typedef struct VQUEUEELEM
168{
169 uint32_t uIndex;
170 uint32_t cIn;
171 uint32_t cOut;
172 VQUEUESEG aSegsIn[VRING_MAX_SIZE];
173 VQUEUESEG aSegsOut[VRING_MAX_SIZE];
174} VQUEUEELEM;
175typedef VQUEUEELEM *PVQUEUEELEM;
176
177
178enum VirtioDeviceType
179{
180 VIRTIO_NET_ID = 0,
181 VIRTIO_BLK_ID = 1,
182 VIRTIO_32BIT_HACK = 0x7fffffff
183};
184
185
186/**
187 * The core shared state of a VirtIO PCI device
188 */
189typedef struct VPCISTATE
190{
191 PDMCRITSECT cs; /**< Critical section - what is it protecting? */
192 /** Read-only part, never changes after initialization. */
193 char szInstance[8]; /**< Instance name, e.g. VNet#1. */
194
195 /* Read/write part, protected with critical section. */
196 /** Status LED. */
197 PDMLED led;
198
199 uint32_t uGuestFeatures;
200 uint16_t uQueueSelector; /**< An index in aQueues array. */
201 uint8_t uStatus; /**< Device Status (bits are device-specific). */
202 uint8_t uISR; /**< Interrupt Status Register. */
203
204 /** Number of queues actually used. */
205 uint32_t cQueues;
206 uint32_t u32Padding;
207 /** Shared queue data. */
208 VQUEUE Queues[VIRTIO_MAX_NQUEUES];
209
210 STAMCOUNTER StatIntsRaised;
211 STAMCOUNTER StatIntsSkipped;
212
213#ifdef VBOX_WITH_STATISTICS
214 STAMPROFILEADV StatIOReadR3;
215 STAMPROFILEADV StatIOReadR0;
216 STAMPROFILEADV StatIOReadRC;
217 STAMPROFILEADV StatIOWriteR3;
218 STAMPROFILEADV StatIOWriteR0;
219 STAMPROFILEADV StatIOWriteRC;
220#endif
221} VPCISTATE;
222
223
224/**
225 * The core ring-3 state of a VirtIO PCI device
226 *
227 * @implements PDMILEDPORTS
228 */
229typedef struct VPCISTATER3
230{
231 /** Status LUN: Base interface. */
232 PDMIBASE IBase;
233 /** Status LUN: LED port interface. */
234 PDMILEDPORTS ILeds;
235 /** Status LUN: LED connector (peer). */
236 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
237 /** Pointer to the shared state. */
238 R3PTRTYPE(PVPCISTATE) pShared;
239 /** Ring-3 per-queue data. */
240 VQUEUER3 Queues[VIRTIO_MAX_NQUEUES];
241} VPCISTATER3;
242
243
244/**
245 * The core ring-0 state of a VirtIO PCI device
246 */
247typedef struct VPCISTATER0
248{
249 uint64_t uUnused;
250} VPCISTATER0;
251
252
253/**
254 * The core raw-mode state of a VirtIO PCI device
255 */
256typedef struct VPCISTATERC
257{
258 uint64_t uUnused;
259} VPCISTATERC;
260
261
262/** @name VirtIO port I/O callbacks.
263 * @{ */
264typedef struct VPCIIOCALLBACKS
265{
266 DECLCALLBACKMEMBER(uint32_t, pfnGetHostFeatures,(PVPCISTATE pVPciState));
267 DECLCALLBACKMEMBER(uint32_t, pfnGetHostMinimalFeatures,(PVPCISTATE pVPciState));
268 DECLCALLBACKMEMBER(void, pfnSetHostFeatures,(PVPCISTATE pVPciState, uint32_t fFeatures));
269 DECLCALLBACKMEMBER(int, pfnGetConfig,(PVPCISTATE pVPciState, uint32_t offCfg, uint32_t cb, void *pvData));
270 DECLCALLBACKMEMBER(int, pfnSetConfig,(PVPCISTATE pVPciState, uint32_t offCfg, uint32_t cb, void *pvData));
271 DECLCALLBACKMEMBER(int, pfnReset,(PPDMDEVINS pDevIns));
272 DECLCALLBACKMEMBER(void, pfnReady,(PPDMDEVINS pDevIns));
273} VPCIIOCALLBACKS;
274/** Pointer to a const VirtIO port I/O callback structure. */
275typedef const VPCIIOCALLBACKS *PCVPCIIOCALLBACKS;
276/** @} */
277
278int vpciR3Init(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVPCISTATECC pThisCC, uint16_t uDeviceId, uint16_t uClass, uint32_t cQueues);
279int vpciRZInit(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVPCISTATECC pThisCC);
280int vpciR3Term(PPDMDEVINS pDevIns, PVPCISTATE pThis);
281PVQUEUE vpciR3AddQueue(PVPCISTATE pThis, PVPCISTATECC pThisCC, unsigned uSize, PFNVPCIQUEUECALLBACK pfnCallback, const char *pcszName);
282void *vpciR3QueryInterface(PVPCISTATECC pThisCC, const char *pszIID);
283void vpciR3SetWriteLed(PVPCISTATE pThis, bool fOn);
284void vpciR3SetReadLed(PVPCISTATE pThis, bool fOn);
285int vpciR3SaveExec(PCPDMDEVHLPR3 pHlp, PVPCISTATE pThis, PSSMHANDLE pSSM);
286int vpciR3LoadExec(PCPDMDEVHLPR3 pHlp, PVPCISTATE pThis, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass, uint32_t cQueues);
287void vpciR3DumpStateWorker(PVPCISTATE pThis, PCDBGFINFOHLP pHlp);
288
289void vpciReset(PPDMDEVINS pDevIns, PVPCISTATE pThis);
290int vpciRaiseInterrupt(PPDMDEVINS pDevIns, PVPCISTATE pThis, int rcBusy, uint8_t u8IntCause);
291int vpciIOPortIn(PPDMDEVINS pDevIns, PVPCISTATE pThis, RTIOPORT offPort,
292 uint32_t *pu32, unsigned cb,PCVPCIIOCALLBACKS pCallbacks);
293int vpciIOPortOut(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVPCISTATECC pThisCC, RTIOPORT offPort,
294 uint32_t u32, unsigned cb, PCVPCIIOCALLBACKS pCallbacks);
295
296#define VPCI_CS
297
298#ifdef VPCI_CS
299# define VPCI_R3_CS_ENTER_RETURN_VOID(a_pDevIns, a_pThis) do { \
300 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &(a_pThis)->cs, VERR_IGNORED); \
301 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &(a_pThis)->cs, rcLock); \
302 } while (0)
303#else
304# define VPCI_R3_CS_ENTER_RETURN_VOID(a_pDevIns, a_pThis) do { } while (0)
305#endif
306
307DECLINLINE(int) vpciCsEnter(PPDMDEVINS pDevIns, PVPCISTATE pThis, int rcBusy)
308{
309#ifdef VPCI_CS
310 return PDMDevHlpCritSectEnter(pDevIns, &pThis->cs, rcBusy);
311#else
312 RT_NOREF(pDevIns, pThis, rcBusy);
313 return VINF_SUCCESS;
314#endif
315}
316
317DECLINLINE(void) vpciCsLeave(PPDMDEVINS pDevIns, PVPCISTATE pThis)
318{
319#ifdef VPCI_CS
320 PDMDevHlpCritSectLeave(pDevIns, &pThis->cs);
321#endif
322}
323
324void vringSetNotification(PPDMDEVINS pDevIns, PVRING pVRing, bool fEnabled);
325
326DECLINLINE(uint16_t) vringReadAvailIndex(PPDMDEVINS pDevIns, PVRING pVRing)
327{
328 uint16_t idx = 0;
329 PDMDevHlpPhysRead(pDevIns, pVRing->addrAvail + RT_UOFFSETOF(VRINGAVAIL, uNextFreeIndex), &idx, sizeof(idx));
330 return idx;
331}
332
333bool vqueueSkip(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVQUEUE pQueue);
334bool vqueueGet(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVQUEUE pQueue, PVQUEUEELEM pElem, bool fRemove = true);
335void vqueuePut(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVQUEUE pQueue, PVQUEUEELEM pElem, uint32_t uLen, uint32_t uReserved = 0);
336void vqueueSync(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVQUEUE pQueue);
337
338DECLINLINE(bool) vqueuePeek(PPDMDEVINS pDevIns, PVPCISTATE pThis, PVQUEUE pQueue, PVQUEUEELEM pElem)
339{
340 return vqueueGet(pDevIns, pThis, pQueue, pElem, /* fRemove */ false);
341}
342
343DECLINLINE(bool) vqueueIsReady(PVQUEUE pQueue)
344{
345 return !!pQueue->VRing.addrAvail;
346}
347
348DECLINLINE(bool) vqueueIsEmpty(PPDMDEVINS pDevIns, PVQUEUE pQueue)
349{
350 return vringReadAvailIndex(pDevIns, &pQueue->VRing) == pQueue->uNextAvailIndex;
351}
352
353#endif /* !VBOX_INCLUDED_SRC_VirtIO_Virtio_h */
354
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