DevIoApic.cpp revision 123e1314b20b799df4be8bb62a624cee8f1e5695
/* $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
/**
* 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)
{
rc = PDMDevHlpMMIORegisterRC(pDevIns, UINT32_C(0xfec00000), 0x1000, NIL_RTRCPTR /*pvUser*/, "ioapicMMIOWrite", "ioapicMMIORead");
"ioapicMMIOWrite", "ioapicMMIORead");
}
rc = PDMDevHlpSSMRegister(pDevIns, 1 /* version */, sizeof(*pThis), ioapicSaveExec, ioapicLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Register debugger info callback.
*/
#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 */
/* 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 */