DevPIC.cpp revision 1cde4dd19cba0507a9cdab737272d88feba05d41
/* $Id$ */
/** @file
* DevPIC - Intel 8259 Programmable Interrupt Controller (PIC) Device.
*/
/*
* Copyright (C) 2006-2007 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_PIC
#include "VBoxDD.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def PIC_LOCK
* Acquires the PDM lock. This is a NOP if locking is disabled. */
/** @def PIC_UNLOCK
* Releases the PDM lock. This is a NOP if locking is disabled. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
} while (0)
#define PIC_UNLOCK(pThis) \
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
PDMBOTHCBDECL(int) picIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) picIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
PDMBOTHCBDECL(int) picIOPortElcrRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) picIOPortElcrWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
/*
* QEMU 8259 interrupt controller emulation
*
* Copyright (c) 2003-2004 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* debug PIC */
#define DEBUG_PIC
/*#define DEBUG_IRQ_COUNT*/
typedef struct PicState {
/** Pointer to the device instance, R3 Ptr. */
/** Pointer to the device instance, R0 Ptr. */
/** Pointer to the device instance, RC Ptr. */
/** The IRQ tags and source IDs for each (tracing purposes). */
} PicState;
/**
* A PIC device instance data.
*/
typedef struct DEVPIC
{
/** The two interrupt controllers. */
/** Pointer to the device instance - R3 Ptr. */
/** Pointer to the PIC R3 helpers. */
/** Pointer to the device instance - R0 Ptr. */
/** Pointer to the PIC R0 helpers. */
/** Pointer to the device instance - RC Ptr. */
/** Pointer to the PIC RC helpers. */
/** Number of release log entries. Used to prevent flooding. */
#ifdef VBOX_WITH_STATISTICS
#endif
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef LOG_ENABLED
{
Log2(("%s: pic%d: elcr=%x last_irr=%x irr=%x imr=%x isr=%x irq_base=%x\n",
}
#else
#endif
/* set irq level. If an edge is detected, then the IRR is set to 1 */
{
int mask;
/* level triggered */
if (level) {
} else {
}
} else {
/* edge triggered */
if (level) {
{
}
} else {
}
}
/* Save the tag. */
if (level)
{
else
}
DumpPICState(s, "pic_set_irq1");
}
/* return the highest priority found in mask (highest = smallest
number). Return 8 if no irq */
{
int priority;
if (mask == 0)
return 8;
priority = 0;
priority++;
return priority;
}
/* return the pic wanted interrupt. return -1 if none */
static int pic_get_irq(PicState *s)
{
DumpPICState(s, "pic_get_irq");
if (priority == 8)
return -1;
/* compute current priority. If special fully nested mode on the
master, the IRQ coming from the slave is not taken into account
for the priority computation. */
if (s->special_fully_nested_mode && s == &pics[0])
Log(("pic_get_irq%d: cur_priority=%x pending=%d\n", (s == pics) ? 0 : 1, cur_priority, (priority == 8) ? -1 : (priority + s->priority_add) & 7));
if (priority < cur_priority) {
/* higher priority found: an irq should be generated */
} else {
return -1;
}
}
/* raise irq to CPU if necessary. must be called every time the active
irq may change */
{
/* first look at slave pic */
if (irq2 >= 0) {
/* if irq request by slave pic, signal master PIC */
} else {
/* If not, clear the IR on the master PIC. */
}
/* look at requested irq */
if (irq >= 0)
{
/* If irq 2 is pending on the master pic, then there must be one pending on the slave pic too! Otherwise we'll get
* spurious slave interrupts in picGetInterrupt.
*/
{
#if defined(DEBUG_PIC)
int i;
for(i = 0; i < 2; i++) {
Log(("pic%d: imr=%x irr=%x padd=%d\n",
pics[i].priority_add));
}
Log(("pic: cpu_interrupt\n"));
#endif
}
else
{
Log(("pic_update_irq: irq 2 is active, but no interrupt is pending on the slave pic!!\n"));
/* Clear it here, so lower priority interrupts can still be dispatched. */
/* if this was the only pending irq, then we must clear the interrupt ff flag */
/** @note Is this correct? */
/* Call ourselves again just in case other interrupts are pending */
return pic_update_irq(pThis);
}
}
else
{
Log(("pic_update_irq: no interrupt is pending!!\n"));
/* we must clear the interrupt ff flag */
}
return VINF_SUCCESS;
}
/** @note if an interrupt line state changes from unmasked to masked, then it must be deactivated when currently pending! */
{
/* Query the current pending irq, if any. */
if (irq == 2)
{
}
/* Update IMR */
/* If an interrupt is pending and now masked, then clear the FF flag. */
if ( irq >= 0
{
Log(("pic_update_imr: pic0: elcr=%x last_irr=%x irr=%x imr=%x isr=%x irq_base=%x\n",
pThis->aPics[0].elcr, pThis->aPics[0].last_irr, pThis->aPics[0].irr, pThis->aPics[0].imr, pThis->aPics[0].isr, pThis->aPics[0].irq_base));
Log(("pic_update_imr: pic1: elcr=%x last_irr=%x irr=%x imr=%x isr=%x irq_base=%x\n",
pThis->aPics[1].elcr, pThis->aPics[1].last_irr, pThis->aPics[1].irr, pThis->aPics[1].imr, pThis->aPics[1].isr, pThis->aPics[1].irq_base));
/* Clear pending IRQ 2 on master controller in case of slave interrupt. */
/** @todo Is this correct? */
if (intno > 7)
{
}
else
}
}
/**
* Set the an IRQ.
*
* @param pDevIns Device instance of the PICs.
* @param iIrq IRQ number to set.
* @param iLevel IRQ level.
* @param uTagSrc The IRQ tag and source ID (for tracing).
*/
{
{
/* A flip-flop lowers the IRQ line and immediately raises it, so
* that a rising edge is guaranteed to occur. Note that the IRQ
* line must be held high for a while to avoid spurious interrupts.
*/
}
}
/* acknowledge interrupt 'irq' */
{
if (s->auto_eoi) {
if (s->rotate_on_auto_eoi)
} else {
}
/* We don't clear a level sensitive interrupt here */
{
}
}
/**
* Get a pending interrupt.
*
* @returns Pending interrupt number.
* @param pDevIns Device instance of the PICs.
* @param puTagSrc Where to return the IRQ tag and source ID.
*/
{
int irq;
int irq2;
int intno;
/* read the irq from the PIC */
if (irq >= 0)
{
if (irq == 2)
{
if (irq2 >= 0) {
}
else
{
/* Interrupt went away or is now masked. */
Log(("picGetInterrupt: spurious IRQ on slave controller, converted to IRQ15\n"));
irq2 = 7;
}
Log2(("picGetInterrupt1: %x base=%x irq=%x uTagSrc=%#x\n", intno, pThis->aPics[1].irq_base, irq2, *puTagSrc));
}
else
{
Log2(("picGetInterrupt0: %x base=%x irq=%x uTagSrc=%#x\n", intno, pThis->aPics[0].irq_base, irq, *puTagSrc));
}
}
else
{
/* Interrupt went away or is now masked. */
Log(("picGetInterrupt: spurious IRQ on master controller, converted to IRQ7\n"));
irq = 7;
*puTagSrc = 0;
}
Log(("picGetInterrupt: 0x%02x pending 0:%d 1:%d\n", intno, pic_get_irq(&pThis->aPics[0]), pic_get_irq(&pThis->aPics[1])));
return intno;
}
{
}
{
int rc = VINF_SUCCESS;
addr &= 1;
if (addr == 0) {
if (val & 0x10) {
/* init */
pic_reset(s);
/* deassert a pending interrupt */
s->init_state = 1;
if (val & 0x02)
AssertReleaseMsgFailed(("single mode not supported"));
if (val & 0x08)
LogRel(("pic_write: Level sensitive IRQ setting ignored.\n"));
} else if (val & 0x08) {
if (val & 0x04)
s->poll = 1;
if (val & 0x02)
if (val & 0x40)
} else {
switch(cmd) {
case 0:
case 4:
break;
case 1: /* end of interrupt */
case 5:
{
if (priority != 8) {
if (cmd == 5)
DumpPICState(s, "eoi");
}
break;
}
case 3:
{
DumpPICState(s, "eoi2");
break;
}
case 6:
{
break;
}
case 7:
{
DumpPICState(s, "eoi3");
break;
}
default:
/* no operation */
break;
}
}
} else {
switch(s->init_state) {
case 0:
{
/* normal mode */
break;
}
case 1:
s->init_state = 2;
break;
case 2:
if (s->init4) {
s->init_state = 3;
} else {
s->init_state = 0;
}
break;
case 3:
s->init_state = 0;
Log(("pic_write: special_fully_nested_mode=%d auto_eoi=%d\n", s->special_fully_nested_mode, s->auto_eoi));
break;
}
}
return rc;
}
{
int ret;
ret = pic_get_irq(s);
if (ret >= 0) {
if (addr1 >> 7) {
Log2(("pic_poll_read: clear slave irq (isr)\n"));
}
} else {
ret = 0;
}
return ret;
}
{
unsigned int addr;
int ret;
*pRC = VINF_SUCCESS;
addr &= 1;
if (s->poll) {
s->poll = 0;
} else {
if (addr == 0) {
if (s->read_reg_select)
else
} else {
}
}
return ret;
}
/* -=-=-=-=-=- wrappers / stuff -=-=-=-=-=- */
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - pointer to the PIC in question.
* @param uPort Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) picIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
int rc;
return rc;
}
return VERR_IOM_IOPORT_UNUSED;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - pointer to the PIC in question.
* @param uPort Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) picIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
if (cb == 1)
{
int rc;
return rc;
}
return VINF_SUCCESS;
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - pointer to the PIC in question.
* @param uPort Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) picIOPortElcrRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
if (cb == 1)
{
return VINF_SUCCESS;
}
return VERR_IOM_IOPORT_UNUSED;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - pointer to the PIC in question.
* @param uPort Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) picIOPortElcrWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
if (cb == 1)
{
}
return VINF_SUCCESS;
}
#ifdef IN_RING3
/**
* PIC status info callback.
*
* @param pDevIns The device instance.
* @param pHlp The output helpers.
* @param pszArgs The arguments.
*/
{
/*
* Show info.
*/
for (int i = 0; i < 2; i++)
{
}
}
/**
* Saves a state of the programmable interrupt controller device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
{
{
}
return VINF_SUCCESS;
}
/**
* Loads a saved programmable interrupt controller device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) picLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
{
if (uVersion != 1)
{
}
return VINF_SUCCESS;
}
/* -=-=-=-=-=- real code -=-=-=-=-=- */
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
unsigned i;
LogFlow(("picReset:\n"));
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
unsigned i;
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
int rc;
bool fGCEnabled;
bool fR0Enabled;
/*
* Validate and read configuration.
*/
if (RT_FAILURE(rc))
N_("Configuration error: failed to read GCEnabled as boolean"));
if (RT_FAILURE(rc))
N_("Configuration error: failed to read R0Enabled as boolean"));
/*
* Init the data.
*/
pThis->cRelLogEntries = 0;
/*
* Register us as the PIC with PDM.
*/
if (fGCEnabled)
{
}
else
{
}
if (fR0Enabled)
{
}
else
{
}
if (fGCEnabled)
if (fR0Enabled)
/*
* Since the PIC helper interface provides access to the PDM lock,
* we need no device level critical section.
*/
/*
* Register I/O ports and save state.
*/
rc = PDMDevHlpIOPortRegister(pDevIns, 0x20, 2, (void *)0, picIOPortWrite, picIOPortRead, NULL, NULL, "i8259 PIC #0");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegister(pDevIns, 0xa0, 2, (void *)1, picIOPortWrite, picIOPortRead, NULL, NULL, "i8259 PIC #1");
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x20, 2, 0, "picIOPortWrite", "picIOPortRead", NULL, NULL, "i8259 PIC #0");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0xa0, 2, 1, "picIOPortWrite", "picIOPortRead", NULL, NULL, "i8259 PIC #1");
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled)
{
rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x20, 2, 0, "picIOPortWrite", "picIOPortRead", NULL, NULL, "i8259 PIC #0");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0xa0, 2, 1, "picIOPortWrite", "picIOPortRead", NULL, NULL, "i8259 PIC #1");
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
if (fGCEnabled)
{
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
}
if (fR0Enabled)
{
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
/*
* Register the info item.
*/
/*
* Initialize the device state.
*/
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqGC, STAMTYPE_COUNTER, "/Devices/PIC/SetIrqGC", STAMUNIT_OCCURENCES, "Number of PIC SetIrq calls in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqHC, STAMTYPE_COUNTER, "/Devices/PIC/SetIrqHC", STAMUNIT_OCCURENCES, "Number of PIC SetIrq calls in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveIRQ2, STAMTYPE_COUNTER, "/Devices/PIC/Masked/ActiveIRQ2", STAMUNIT_OCCURENCES, "Number of cleared irq 2.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveMasterIRQ, STAMTYPE_COUNTER, "/Devices/PIC/Masked/ActiveMaster", STAMUNIT_OCCURENCES, "Number of cleared master irqs.");
PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveSlaveIRQ, STAMTYPE_COUNTER, "/Devices/PIC/Masked/ActiveSlave", STAMUNIT_OCCURENCES, "Number of cleared slave irqs.");
#endif
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceI8259 =
{
/* u32Version */
/* szName */
"i8259",
/* szRCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Intel 8259 Programmable Interrupt Controller (PIC) 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(DEVPIC),
/* 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 */