DevIoApic.cpp revision d847fce8adf688a490e38c218e2d9446c002cafd
/* $Id$ */
/** @file
* I/O Advanced Programmable Interrupt Controller (IO-APIC) Device.
*/
/*
* Copyright (C) 2006-2013 Oracle Corporation
*
* 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.
* --------------------------------------------------------------------
*
* This code is based on:
*
* apic.c revision 1.5 @@OSETODO
*
* APIC support
*
* Copyright (c) 2004-2005 Fabrice Bellard
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_APIC
#include "VBoxDD2.h"
#include "DevApic.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def IOAPIC_LOCK
* Acquires the PDM lock. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
} while (0)
/** @def IOAPIC_UNLOCK
* Releases the PDM lock. */
#define DEBUG_IOAPIC
#define IOAPIC_NUM_PINS 0x18
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct IOAPIC
{
/** The IRQ tags and source IDs for each pin (tracing purposes). */
/** The device instance - R3 Ptr. */
/** The IOAPIC helpers - R3 Ptr. */
/** The device instance - R0 Ptr. */
/** The IOAPIC helpers - R0 Ptr. */
/** The device instance - RC Ptr. */
/** The IOAPIC helpers - RC Ptr. */
# ifdef VBOX_WITH_STATISTICS
# endif
} IOAPIC;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
{
uint8_t i;
for (i = 0; i < IOAPIC_NUM_PINS; i++)
{
mask = 1 << i;
{
if (!(entry & APIC_LVT_MASKED))
{
if (trig_mode == APIC_TRIGGER_EDGE)
{
}
if (delivery_mode == APIC_DM_EXTINT)
/* malc: i'm still not so sure about ExtINT delivery */
{
AssertMsgFailed(("Delivery mode ExtINT"));
}
else
dest,
uTagSrc);
/* We must be sure that attempts to reschedule in R3
never get here */
}
}
}
}
{
{
{
/* level triggered */
if (level)
{
else
{
}
}
else
{
}
}
else
{
/* edge triggered */
if (level)
{
else
}
}
}
}
/**
* Handles a read from the IOAPICID register.
*/
{
return VINF_SUCCESS;
}
/**
* Handles a write to the IOAPICID register.
*/
{
/* Note! Compared to the 82093AA spec, we've extended the IOAPIC
identification from bits 27:24 to bits 31:24. */
return VINF_SUCCESS;
}
/**
* Handles a read from the IOAPICVER register.
*/
{
return VINF_SUCCESS;
}
/**
* Handles a read from the IOAPICARB register.
*/
{
*pu32Value = 0; /* (arbitration winner) */
return VINF_SUCCESS;
}
/**
* Handles a read from the IOREGSEL register.
*/
{
return VINF_SUCCESS;
}
/**
* Handles a write to the IOREGSEL register.
*/
{
/* Bits 7:0 are writable, the rest aren't. Confirmed on recent AMD box. */
return VINF_SUCCESS;
}
/**
* Handles a write to the IOWIN register.
*/
{
int rc = VINF_SUCCESS;
if (uIoRegSel == 0)
else if (uIoRegSel == 1)
else if (uIoRegSel == 2)
/*
* IOREDTBL0..IOREDTBL23.
*/
{
if (!(uIoRegSel & 1))
else
}
else
{
*pu32Value = UINT32_MAX;
}
return rc;
}
/**
* Handles a write to the IOWIN register.
*/
{
int rc = VINF_SUCCESS;
/*
* IOAPICID.
*/
if (uIoRegSel == 0)
/*
* IOREDTBL0..IOREDTBL23.
*/
{
if (!(uIoRegSel & 1))
{
/*
* Low DWORD.
*
* Have to do some sanity checks here because Linux 2.6 kernels
* writes seemingly bogus value (u32Value = 0) in their
* unlock_ExtINT_logic() function. Not sure what it's good for, but
* we ran into trouble with INTVEC = 0. Luckily the 82093AA specs
* limits the INTVEC range to 0x10 thru 0xfe, so we use this to
* ignore harmful values.
*
* Update: Looking at real hw (recent AMD), they don't reject
* invalid vector numbers, at least not at this point. Could be that
* some other code path needs to refuse something instead. Results:
* - Writing 0 to lo+hi -> 0.
* - Writing ~0 to lo+hi -> 0xff0000000001afff.
* - Writing ~0 w/ DELMOD set to 011b or 110b (both reserved)
* results in DELMOD containing the reserved values.
* - Ditto with same + DELMOD in [0..7], DELMOD is stored as written.
*/
if ( (u32Value & APIC_LVT_MASKED)
u64NewValue = (pThis->ioredtbl[idxIoRedTbl] & (UINT64_C(0xffffffff00000000) | RT_BIT(14) | RT_BIT(12)))
else
{
LogRel(("IOAPIC GUEST BUG: bad vector writing %x(sel=%x) to %u\n", u32Value, uIoRegSel, idxIoRedTbl));
}
}
else
{
/*
* High DWORD.
*/
}
Log(("ioapic: IOREDTBL%u %#018llx -> %#018llx\n", idxIoRedTbl, pThis->ioredtbl[idxIoRedTbl], u64NewValue));
}
/*
* Read-only or unknown registers. Log it.
*/
else if (uIoRegSel == 1)
else if (uIoRegSel == 2)
else
return rc;
}
PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
/*
* Pass it on to the register read handlers.
* (See 0xff comments in ioapicMMIOWrite.)
*/
int rc;
if (offReg == 0)
else if (offReg == 0x10)
else
{
}
return rc;
}
PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
{
/*
* Fetch the value.
*
* We've told IOM to only give us DWORD accesses. Observations on AMD
* indicates that unaligned writes get their missing bytes written as zero.
*/
/*
* The 0xff mask is because we don't really implement the APICBASE register
* in the PIIX3, so if the guest tries to relocate the IOAPIC via PIIX3 we
* won't know. The I/O APIC address is on the form FEC0xy00h, where xy is
* programmable. Masking 0xff means we cover the y. The x would require
* reregistering MMIO memory, which means the guest is out of luck there.
*/
int rc;
if (offReg == 0)
else if (offReg == 0x10)
else
{
rc = VINF_SUCCESS;
}
return rc;
}
{
/* PDM lock is taken here; */ /** @todo add assertion */
}
PDMBOTHCBDECL(void) ioapicSendMsi(PPDMDEVINS pDevIns, RTGCPHYS GCAddr, uint32_t uValue, uint32_t uTagSrc)
{
#if 0
/*
* This bit indicates whether the message should be directed to the
* processor with the lowest interrupt priority among
* processors that can receive the interrupt, ignored ATM.
*/
#endif
dest,
0 /* polarity, n/a */,
uTagSrc);
/* We must be sure that attempts to reschedule in R3
never get here */
}
#ifdef IN_RING3
/** @interface_method_impl{DBGFREGDESC,pfnGet} */
static DECLCALLBACK(int) ioapicDbgReg_IoRegSel_r(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnSet} */
static DECLCALLBACK(int) ioapicDbgReg_IoRegSel_w(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnGet} */
static DECLCALLBACK(int) ioapicDbgReg_IoWin_r(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnSet} */
static DECLCALLBACK(int) ioapicDbgReg_IoWin_w(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnGet} */
static DECLCALLBACK(int) ioapicDbgReg_IoApicVer_r(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnGet} */
static DECLCALLBACK(int) ioapicDbgReg_IoApicArb_r(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
{
}
/** @interface_method_impl{DBGFREGDESC,pfnGet} */
static DECLCALLBACK(int) ioapicDbgReg_IoRedRblN_r(void *pvUser, PCDBGFREGDESC pDesc, PDBGFREGVAL pValue)
{
return VINF_SUCCESS;
}
/** @interface_method_impl{DBGFREGDESC,pfnSet} */
static DECLCALLBACK(int) ioapicDbgReg_IoRedRblN_w(void *pvUser, PCDBGFREGDESC pDesc, PCDBGFREGVAL pValue, PCDBGFREGVAL pfMask)
{
pThis->ioredtbl[pDesc->offRegister] = pValue->u64 | (~pfMask->u64 &pThis->ioredtbl[pDesc->offRegister]);
return VINF_SUCCESS;
}
/** IOREDTBLn sub fields. */
static DBGFREGSUBFIELD const g_aIoRedTblSubs[] =
{
};
/** Register descriptors for DBGF. */
static DBGFREGDESC const g_aRegDesc[] =
{
{ "ioregsel", DBGFREG_END, DBGFREGVALTYPE_U8, 0, 0, ioapicDbgReg_IoRegSel_r, ioapicDbgReg_IoRegSel_w, NULL, NULL },
{ "iowin", DBGFREG_END, DBGFREGVALTYPE_U32, 0, 0, ioapicDbgReg_IoWin_r, ioapicDbgReg_IoWin_w, NULL, NULL },
{ "ioapicver", DBGFREG_END, DBGFREGVALTYPE_U32, DBGFREG_FLAGS_READ_ONLY, 0, ioapicDbgReg_IoApicVer_r, NULL, NULL, NULL },
{ "ioapicarb", DBGFREG_END, DBGFREGVALTYPE_U32, DBGFREG_FLAGS_READ_ONLY, 0, ioapicDbgReg_IoApicArb_r, NULL, NULL, NULL },
{ "ioredtbl0", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 0, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl1", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 1, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl2", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 2, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl3", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 3, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl4", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 4, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl5", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 5, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl6", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 6, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl7", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 7, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl8", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 8, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl9", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 9, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl10", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 10, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl11", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 11, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl12", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 12, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl13", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 13, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl14", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 14, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl15", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 15, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl16", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 16, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl17", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 17, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl18", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 18, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl19", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 19, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl20", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 20, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl21", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 21, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl22", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 22, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
{ "ioredtbl23", DBGFREG_END, DBGFREGVALTYPE_U64, 0, 23, ioapicDbgReg_IoRedRblN_r, ioapicDbgReg_IoRedRblN_w, NULL, &g_aIoRedTblSubs[0] },
};
/**
* Info handler, device version. Dumps I/O APIC state.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
{
pHlp->pfnPrintf(pHlp, " idx dst_mode dst_addr mask trigger rirr polarity dlvr_st dlvr_mode vector\n");
for (unsigned i = 0; i <= iLastRedir; ++i)
{
static const char * const s_apszDModes[] =
{
"Fixed ", "LowPri", "SMI ", "Resrvd", "NMI ", "INIT ", "Resrvd", "ExtINT"
};
i,
);
}
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
for (unsigned i = 0; i < IOAPIC_NUM_PINS; i++)
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) ioapicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
if (uVersion != 1)
for (unsigned i = 0; i < IOAPIC_NUM_PINS; i++)
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRESET
*/
{
for (unsigned i = 0; i < IOAPIC_NUM_PINS; i++)
{
}
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
/*
* Initialize the state data.
*/
/* (the rest is done by the reset call at the end) */
/* PDM provides locking via the IOAPIC helpers. */
/*
* Validate and read the configuration.
*/
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query integer value \"NumCPUs\""));
bool fRZEnabled;
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"RZEnabled\""));
/*
* Register the IOAPIC and get helpers.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Register MMIO callbacks and saved state.
* Note! The write ZEROing was observed on a real AMD system.
*/
if (RT_FAILURE(rc))
return rc;
if (fRZEnabled)
{
"ioapicMMIOWrite", "ioapicMMIORead");
"ioapicMMIOWrite", "ioapicMMIORead");
}
rc = PDMDevHlpSSMRegister(pDevIns, 1 /* version */, sizeof(*pThis), ioapicSaveExec, ioapicLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Register debugger info callback.
*/
rc = PDMDevHlpDBGFInfoRegister(pDevIns, "ioapic", "Display I/O APIC state.", ioapicInfo); AssertRC(rc);
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in HC.");
#endif
/*
* Reset the device state.
*/
return VINF_SUCCESS;
}
/**
* IO APIC device registration structure.
*/
const PDMDEVREG g_DeviceIOAPIC =
{
/* u32Version */
/* szName */
"ioapic",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"I/O Advanced Programmable Interrupt Controller (IO-APIC) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(IOAPIC),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
/* pfnMemSetup */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */