DevIoApic.cpp revision fb9af443dbf06990f4956d683286ddce29c4dca6
/* $Id$ */
/** @file
* I/O Advanced Programmable Interrupt Controller (IO-APIC) Device.
*/
/*
* Copyright (C) 2006-2010 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. */
do { \
{ \
{ \
code; \
} \
apic++; \
} \
} while (0)
#define DEBUG_IOAPIC
#define IOAPIC_NUM_PINS 0x18
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
struct IOAPICState {
/** 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
};
typedef struct IOAPICState IOAPICState;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static void ioapic_service(IOAPICState *s)
{
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,
/* We must be sure that attempts to reschedule in R3
never get here */
}
}
}
}
{
/* level triggered */
if (level) {
ioapic_service(s);
}
} else {
}
} else {
/* edge triggered */
if (level) {
ioapic_service(s);
}
}
}
}
{
int index;
addr &= 0xff;
if (addr == 0x00) {
} else if (addr == 0x10) {
switch (s->ioregsel) {
case 0x00:
break;
case 0x01:
break;
case 0x02:
val = 0;
break;
default:
if (s->ioregsel & 1)
else
}
}
#ifdef DEBUG_IOAPIC
#endif
}
return val;
}
{
int index;
addr &= 0xff;
if (addr == 0x00) {
return;
} else if (addr == 0x10) {
#ifdef DEBUG_IOAPIC
#endif
switch (s->ioregsel) {
case 0x00:
return;
case 0x01:
case 0x02:
return;
default:
if (s->ioregsel & 1) {
} else {
/* According to IOAPIC spec, vectors should be from 0x10 to 0xfe */
if ((val & APIC_LVT_MASKED) ||
{
}
else
{
/*
* Linux 2.6 kernels has pretty strange function
* unlock_ExtINT_logic() which writes
* absolutely bogus (all 0) value into the vector
* with pretty vague explanation why.
* So we just ignore such writes.
*/
}
}
ioapic_service(s);
}
}
}
}
#ifdef IN_RING3
{
int i;
SSMR3PutU8(f, s->id);
SSMR3PutU8(f, s->ioregsel);
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
SSMR3PutU64(f, s->ioredtbl[i]);
}
}
{
int i;
if (version_id != 1)
SSMR3GetU8(f, &s->id);
SSMR3GetU8(f, &s->ioregsel);
for (i = 0; i < IOAPIC_NUM_PINS; i++) {
SSMR3GetU64(f, &s->ioredtbl[i]);
}
return 0;
}
static void ioapic_reset(void *opaque)
{
int i;
memset(s, 0, sizeof(*s));
for(i = 0; i < IOAPIC_NUM_PINS; i++)
if (pDevIns)
{
}
if (pIoApicHlp)
{
s->pIoApicHlpR3 = pIoApicHlp;
}
}
#endif /* IN_RING3 */
/* IOAPIC */
PDMBOTHCBDECL(int) ioapicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
switch (cb) {
case 1:
break;
case 2:
break;
case 4:
break;
default:
IOAPIC_UNLOCK(s);
return VERR_INTERNAL_ERROR;
}
IOAPIC_UNLOCK(s);
return VINF_SUCCESS;
}
PDMBOTHCBDECL(int) ioapicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
{
switch (cb)
{
default:
IOAPIC_UNLOCK(s);
return VERR_INTERNAL_ERROR;
}
IOAPIC_UNLOCK(s);
return VINF_SUCCESS;
}
{
/* PDM lock is taken here; @todo add assertion */
}
{
#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 */,
/* We must be sure that attempts to reschedule in R3
never get here */
}
#ifdef IN_RING3
/**
* 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.
*/
{
unsigned i;
unsigned max_redir;
val = s->id << 24; /* Would be nice to call ioapic_mem_readl() directly, but that's not so simple. */
val = 0;
pHlp->pfnPrintf(pHlp, " idx dst_mode dst_addr mask trigger rirr polarity dlvr_st dlvr_mode vector\n");
for (i = 0; i <= max_redir; ++i)
{
"NMI ", "INIT ", "Resrvd", "ExtINT" };
i,
s->ioredtbl[i] /* entire register */
);
}
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
ioapic_save(pSSM, s);
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) ioapicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
AssertFailed();
}
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRESET
*/
{
ioapic_reset(s);
IOAPIC_UNLOCK(s);
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
bool fGCEnabled;
bool fR0Enabled;
int rc;
/*
* Validate and read the configuration.
*/
if (!CFGMR3AreValuesValid(pCfg,
"GCEnabled\0"
"R0Enabled\0"
"NumCPUs\0"))
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"GCEnabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"R0Enabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query integer value \"NumCPUs\""));
/*
* Initialize the state data.
*/
ioapic_reset(s);
/* PDM provides locking via the IOAPIC helpers. */
/*
* Register the IOAPIC and get helpers.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Register MMIO callbacks and saved state.
*/
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled) {
rc = PDMDevHlpMMIORegisterRC(pDevIns, 0xfec00000, 0x1000, NIL_RTRCPTR /*pvUser*/, "ioapicMMIOWrite", "ioapicMMIORead");
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled) {
"ioapicMMIOWrite", "ioapicMMIORead");
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
/*
* Register debugger info callback.
*/
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOReadHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO reads in HC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatMMIOWriteHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of IOAPIC MMIO writes in HC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqGC, STAMTYPE_COUNTER, "/Devices/IOAPIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in GC.");
PDMDevHlpSTAMRegister(pDevIns, &s->StatSetIrqHC, STAMTYPE_COUNTER, "/Devices/IOAPIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of IOAPIC SetIrq calls in HC.");
#endif
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(IOAPICState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
/* pfnIOCtl */
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 */