Virtio.cpp revision 38ffbe9d3bbeb49e4100cccc7cbfc38e9703f148
/* $Id$ */
/** @file
* Virtio - Virtio Common Functions (VRing, VQueue, Virtio PCI)
*
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#define LOG_GROUP LOG_GROUP_DEV_VIRTIO
#include "Virtio.h"
#define IFACE_TO_STATE(pIface, ifaceName) ((VPCISTATE *)((char*)pIface - RT_OFFSETOF(VPCISTATE, ifaceName)))
#ifdef DEBUG
#endif /* DEBUG */
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
//RT_C_DECLS_BEGIN
//RT_C_DECLS_END
{
pQueue->uNextAvailIndex = 0;
pQueue->uNextUsedIndex = 0;
pQueue->uPageNumber = 0;
}
{
PAGE_SIZE); /* The used ring must start from the next page. */
pQueue->uNextAvailIndex = 0;
pQueue->uNextUsedIndex = 0;
}
// void vqueueElemFree(PVQUEUEELEM pElem)
// {
// }
{
//Log(("%s vringReadDesc: ring=%p idx=%u\n", INSTANCE(pState), pVRing, uIndex));
}
{
return tmp;
}
{
return tmp;
}
{
if (fEnabled)
tmp &= ~ VRINGUSED_F_NO_NOTIFY;
else
}
{
return false;
do
{
{
}
else
{
}
return true;
}
{
return tmp;
}
{
}
void vringWriteUsedElem(PVPCISTATE pState, PVRING pVRing, uint32_t uIndex, uint32_t uId, uint32_t uLen)
{
}
{
unsigned int i, uOffset;
{
{
QUEUENAME(pState, pQueue), pQueue->uNextUsedIndex, i, pElem->aSegsIn[i].addr, pElem->aSegsIn[i].pv, pElem->aSegsIn[i].cb, cbSegLen));
}
}
QUEUENAME(pState, pQueue), pQueue->uNextUsedIndex, vringReadUsedIndex(pState, &pQueue->VRing), pElem->uIndex, uLen));
}
{
LogFlow(("%s vqueueNotify: %s availFlags=%x guestFeatures=%x vqueue is %sempty\n",
{
if (RT_FAILURE(rc))
}
else
{
}
}
{
}
{
pState->uGuestFeatures = 0;
pState->uQueueSelector = 0;
}
/**
* Raise interrupt.
*
* @param pState The device state structure.
* @param rcBusy Status code to return when the critical section is busy.
* @param u8IntCause Interrupt cause bit mask to set in PCI ISR port.
*/
{
return rc;
LogFlow(("%s vpciRaiseInterrupt: u8IntCause=%x\n",
return VINF_SUCCESS;
}
/**
* Lower interrupt.
*
* @param pState The device state structure.
*/
{
}
{
return pfnGetHostFeatures(pState)
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser Pointer to the device state structure.
* @param port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
* @thread EMT
*/
void *pvUser,
unsigned cb,
{
int rc = VINF_SUCCESS;
switch (port)
{
case VPCI_HOST_FEATURES:
/* Tell the guest what features we support. */
break;
case VPCI_GUEST_FEATURES:
break;
case VPCI_QUEUE_PFN:
break;
case VPCI_QUEUE_NUM:
break;
case VPCI_QUEUE_SEL:
break;
case VPCI_STATUS:
break;
case VPCI_ISR:
break;
default:
if (port >= VPCI_CONFIG)
{
}
else
{
*pu32 = 0xFFFFFFFF;
"no valid port at offset port=%RTiop "
}
break;
}
Log3(("%s vpciIOPortIn: At %RTiop in %0*x\n",
return rc;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
* @thread EMT
*/
void *pvUser,
unsigned cb,
{
int rc = VINF_SUCCESS;
bool fHasBecomeReady;
switch (port)
{
case VPCI_GUEST_FEATURES:
/* Check if the guest negotiates properly, fall back to basics if it does not. */
if (VPCI_F_BAD_FEATURE & u32)
{
Log(("%s WARNING! Guest failed to negotiate properly (guest=%x)\n",
}
/* The guest may potentially desire features we don't support! */
{
Log(("%s Guest asked for features host does not support! (host=%x guest=%x)\n",
}
else
break;
case VPCI_QUEUE_PFN:
/*
* The guest is responsible for allocating the pages for queues,
* here it provides us with the page number of descriptor table.
* Note that we provide the size of the queue to the guest via
* VIRTIO_PCI_QUEUE_NUM.
*/
if (u32)
else
break;
case VPCI_QUEUE_SEL:
u32 &= 0xFFFF;
else
break;
case VPCI_QUEUE_NOTIFY:
#ifdef IN_RING3
u32 &= 0xFFFF;
else
Log(("%s The queue (#%d) being notified has not been initialized.\n",
else
#else
#endif
break;
case VPCI_STATUS:
u32 &= 0xFF;
/* Writing 0 to the status port triggers device reset. */
if (u32 == 0)
else if (fHasBecomeReady)
break;
default:
if (port >= VPCI_CONFIG)
else
rc = PDMDeviceDBGFStop(pDevIns, RT_SRC_POS, "%s vpciIOPortOut: no valid port at offset port=%RTiop cb=%08x\n", szInst, port, cb);
break;
}
return rc;
}
#ifdef IN_RING3
/**
* Provides interfaces to the driver.
*
* @returns Pointer to interface. NULL if the interface is not supported.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread EMT
*/
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_LED_PORTS:
default:
return NULL;
}
}
/**
* Gets the pointer to the status LED of a unit.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure.
* @param iLUN The unit which status LED we desire.
* @param ppLed Where to store the LED pointer.
* @thread EMT
*/
static DECLCALLBACK(int) vpciQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
{
int rc = VERR_PDM_LUN_NOT_FOUND;
if (iLUN == 0)
{
rc = VINF_SUCCESS;
}
return rc;
}
/**
*
* @returns VBox status code.
* @param pState Pointer to the device state structure.
* @param fOn New LED state.
*/
{
if (fOn)
else
}
/**
*
* @returns VBox status code.
* @param pState Pointer to the device state structure.
* @param fOn New LED state.
*/
{
if (fOn)
else
}
/**
* Sets 8-bit register in PCI configuration space.
* @param refPciDev The PCI device.
* @param uOffset The register offset.
* @param u16Value The value to store in the register.
* @thread EMT
*/
{
}
/**
* Sets 16-bit register in PCI configuration space.
* @param refPciDev The PCI device.
* @param uOffset The register offset.
* @param u16Value The value to store in the register.
* @thread EMT
*/
{
}
/**
* Sets 32-bit register in PCI configuration space.
* @param refPciDev The PCI device.
* @param uOffset The register offset.
* @param u32Value The value to store in the register.
* @thread EMT
*/
{
}
#ifdef DEBUG
{
Log2(("vpciDumpState: (called from %s)\n"
" uGuestFeatures = 0x%08x\n"
" uQueueSelector = 0x%04x\n"
" uStatus = 0x%02x\n"
" uISR = 0x%02x\n",
Log2((" %s queue:\n"
" VRing.uSize = %u\n"
" VRing.addrDescriptors = %p\n"
" VRing.addrAvail = %p\n"
" VRing.addrUsed = %p\n"
" uNextAvailIndex = %u\n"
" uNextUsedIndex = %u\n"
" uPageNumber = %x\n",
}
#else
# define vpciDumpState(x, s) do {} while (0)
#endif
/**
* Saves the state of device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
{
int rc;
/* Save queue states */
{
}
return VINF_SUCCESS;
}
/**
* Loads a saved device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
int vpciLoadExec(PVPCISTATE pState, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass, uint32_t nQueues)
{
int rc;
if (uPass == SSM_PASS_FINAL)
{
/* Restore state data */
/* Restore queues */
{
}
else
{
}
}
return VINF_SUCCESS;
}
/**
* Set PCI configuration space registers.
*
* @param pci Reference to PCI device structure.
* @param uSubsystemId PCI Subsystem Id
* @param uClass Class of PCI device (network, etc)
* @thread EMT
*/
{
/* Configure PCI Device, assume 32-bit mode ******************************/
/* ABI version, must be equal 0 as of 2.6.30 kernel. */
/* Ethernet adapter */
/* Interrupt Pin: INTA# */
}
/* WARNING! This function must never be used in multithreaded context! */
static const char *vpciCounter(const char *pszDevFmt,
const char *pszCounter)
{
static char g_szCounterName[80];
return g_szCounterName;
}
// TODO: header
int iInstance, const char *pcszNameFmt,
{
int rc = VINF_SUCCESS;
/* Init handles and log related stuff. */
/* Initialize critical section. */
if (RT_FAILURE(rc))
return rc;
/* Set PCI config registers */
/* Register PCI device */
if (RT_FAILURE(rc))
return rc;
/* Status driver */
if (RT_FAILURE(rc))
pState->pLedsConnector = (PPDMILEDCONNECTORS)pBase->pfnQueryInterface(pBase, PDMINTERFACE_LED_CONNECTORS);
#if defined(VBOX_WITH_STATISTICS)
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIOReadGC, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in GC", vpciCounter(pcszNameFmt, "IO/ReadGC"), iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIOReadHC, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in HC", vpciCounter(pcszNameFmt, "IO/ReadHC"), iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIOWriteGC, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in GC", vpciCounter(pcszNameFmt, "IO/WriteGC"), iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIOWriteHC, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in HC", vpciCounter(pcszNameFmt, "IO/WriteHC"), iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIntsRaised, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of raised interrupts", vpciCounter(pcszNameFmt, "Interrupts/Raised"), iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatIntsSkipped, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of skipped interrupts", vpciCounter(pcszNameFmt, "Interrupts/Skipped"), iInstance);
#endif /* VBOX_WITH_STATISTICS */
return rc;
}
/**
* Destruct PCI-related part of device.
*
* We need to free non-VM resources only.
*
* @returns VBox status.
* @param pState The device state structure.
*/
{
return VINF_SUCCESS;
}
/**
* Device relocation callback.
*
* When this callback is called the device instance data, and if the
* have been changed. The device must use the chance to perform the
* necessary pointer relocations and data updates.
*
* Before the GC code is executed the first time, this function will be
* called with a 0 delta so GC pointer calculations can be one in one place.
*
* @param pDevIns Pointer to the device instance.
* @param offDelta The relocation delta relative to the old location.
*
* @remark A relocation CANNOT fail.
*/
{
// TBD
}
const char *pcszName)
{
/* Find an empty queue slot */
{
{
break;
}
}
if (!pQueue)
{
}
else
{
pQueue->uPageNumber = 0;
}
return pQueue;
}
#endif /* IN_RING3 */
#endif /* VBOX_DEVICE_STRUCT_TESTCASE */