DevAPIC.cpp revision e5da5dbb49b995e6e7d20a79a6cac76307549b15
/* $Id$ */
/** @file
* Advanced Programmable Interrupt Controller (APIC) Device.
*/
/*
* Copyright (C) 2006-2011 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 *
*******************************************************************************/
#define MSR_IA32_APICBASE 0x1b
#ifdef _MSC_VER
#endif
/** The current saved state version.*/
#define APIC_SAVED_STATE_VERSION 3
/** The saved state version used by VirtualBox v3 and earlier.
* This does not include the config. */
#define APIC_SAVED_STATE_VERSION_VBOX_30 2
/** Some ancient version... */
#define APIC_SAVED_STATE_VERSION_ANCIENT 1
/* version 0x14: Pentium 4, Xeon; LVT count depends on that */
#define APIC_HW_VERSION 0x14
/** @def APIC_LOCK
* Acquires the PDM lock. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
} while (0)
/** @def APIC_LOCK_VOID
* Acquires the PDM lock and does not expect failure (i.e. ring-3 only!). */
do { \
} while (0)
/** @def APIC_UNLOCK
* Releases the PDM lock. */
#define APIC_UNLOCK(a_pDev) \
/** @def APIC_AND_TM_LOCK
* Acquires the virtual sync clock lock as well as the PDM lock. */
do { \
if (rc2 != VINF_SUCCESS) \
return rc2; \
if (rc2 != VINF_SUCCESS) \
{ \
return rc2; \
} \
} while (0)
/** @def APIC_AND_TM_UNLOCK
* Releases the PDM lock as well as the TM virtual sync clock lock. */
do { \
} while (0)
/**
* Begins an APIC enumeration block.
*
* Code placed between this and the APIC_FOREACH_END macro will be executed for
* each APIC instance present in the system.
*
* @param a_pDev The APIC device.
*/
#define APIC_FOREACH_BEGIN(a_pDev) \
do { \
{ \
do { } while (0)
/**
* Begins an APIC enumeration block, given a destination set.
*
* Code placed between this and the APIC_FOREACH_END macro will be executed for
* each APIC instance present in @a a_pDstSet.
*
* @param a_pDev The APIC device.
* @param a_pDstSet The destination set.
*/
continue; \
do { } while (0)
/** Counterpart to APIC_FOREACH_IN_SET_BEGIN and APIC_FOREACH_BEGIN. */
#define APIC_FOREACH_END() \
} \
} while (0)
#define DEBUG_APIC
/* APIC Local Vector Table */
#define APIC_LVT_TIMER 0
#define APIC_LVT_THERMAL 1
#define APIC_LVT_PERFORM 2
#define APIC_LVT_LINT0 3
#define APIC_LVT_LINT1 4
#define APIC_LVT_ERROR 5
#define APIC_LVT_NB 6
/* APIC delivery modes */
#define APIC_DM_FIXED 0
#define APIC_DM_LOWPRI 1
#define APIC_DM_SMI 2
#define APIC_DM_NMI 4
#define APIC_DM_INIT 5
#define APIC_DM_SIPI 6
#define APIC_DM_EXTINT 7
/* APIC destination mode */
#define APIC_DESTMODE_FLAT 0xf
#define APIC_DESTMODE_CLUSTER 0x0
#define APIC_TRIGGER_EDGE 0
#define APIC_TRIGGER_LEVEL 1
#define APIC_MAX_PATCH_ATTEMPTS 100
typedef uint32_t PhysApicId;
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct APIC256BITREG
{
/** The bitmap data. */
typedef APIC256BITREG *PAPIC256BITREG;
typedef APIC256BITREG const *PCAPIC256BITREG;
/**
* Tests if a bit in the 256-bit APIC register is set.
*
* @returns true if set, false if clear.
*
* @param pReg The register.
* @param iBit The bit to test for.
*/
{
}
/**
* Sets a bit in the 256-bit APIC register is set.
*
* @param pReg The register.
* @param iBit The bit to set.
*/
{
}
/**
* Clears a bit in the 256-bit APIC register is set.
*
* @param pReg The register.
* @param iBit The bit to clear.
*/
{
}
/**
* Clears all bits in the 256-bit APIC register set.
*
* @param pReg The register.
*/
{
}
/**
* Finds the last bit set in the register, i.e. the highest priority interrupt.
*
* @returns The index of the found bit, @a iRetAllClear if none was found.
*
* @param pReg The register.
* @param iRetAllClear What to return if all bits are clear.
*/
{
while (i-- > 0)
{
if (u)
{
u = ASMBitLastSetU32(u);
u--;
u |= i << 5;
return (int)u;
}
}
return iRetAllClear;
}
typedef struct APICState
{
/* Task priority register (interrupt level) */
/* Logical APIC id - user programmable */
/* Physical APIC id - not visible to user, constant */
/** @todo: is it logical or physical? Not really used anyway now. */
int count_shift;
/** The time stamp of the initial_count load, i.e. when it was started. */
/** The time stamp of the next timer callback. */
/** The APIC timer - R3 Ptr. */
/** The APIC timer - R0 Ptr. */
/** The APIC timer - RC Ptr. */
/** Whether the timer is armed or not */
bool fTimerArmed;
/** Alignment */
bool afAlignment[3];
/** The initial_count value used for the current frequency hint. */
/** The count_shift value used for the current frequency hint. */
/** Timer description timer. */
/** The IRQ tags and source IDs for each (tracing purposes). */
# ifdef VBOX_WITH_STATISTICS
# if HC_ARCH_BITS == 32
# endif
# endif
} APICState;
# ifdef VBOX_WITH_STATISTICS
# endif
typedef struct
{
/** The device instance - R3 Ptr. */
/** The APIC helpers - R3 Ptr. */
/** LAPICs states - R3 Ptr */
/** The critical section - R3 Ptr. */
/** The device instance - R0 Ptr. */
/** The APIC helpers - R0 Ptr. */
/** LAPICs states - R0 Ptr */
/** The critical section - R3 Ptr. */
/** The device instance - RC Ptr. */
/** The APIC helpers - RC Ptr. */
/** LAPICs states - RC Ptr */
/** The critical section - R3 Ptr. */
/** APIC specification version in this virtual hardware configuration. */
/** Number of attempts made to optimize TPR accesses. */
/** Number of CPUs on the system (same as LAPIC count). */
/** Whether we've got an IO APIC or not. */
bool fIoApic;
/** Alignment padding. */
bool afPadding[3];
# ifdef VBOX_WITH_STATISTICS
# endif
# ifdef VBOX_WITH_STATISTICS
# endif
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static PVMCPUSET apic_get_delivery_bitmask(APICDeviceInfo* pDev, uint8_t dest, uint8_t dest_mode, PVMCPUSET pDstSet);
static int apic_get_arb_pri(APICState const *s);
static int apic_get_ppr(APICState const *s);
static void apic_set_irq(APICDeviceInfo* pDev, APICState *s, int vector_num, int trigger_mode, uint32_t uTagSrc);
{
}
{
/* LAPIC's array is indexed by CPU id */
}
{
/* for now we assume LAPIC physical id == CPU id */
}
DECLINLINE(void) cpuSetInterrupt(APICDeviceInfo* pDev, APICState *s, PDMAPICIRQ enmType = PDMAPICIRQ_HARDWARE)
{
getCpuFromLapic(pDev, s));
}
DECLINLINE(void) cpuClearInterrupt(APICDeviceInfo* pDev, APICState *s, PDMAPICIRQ enmType = PDMAPICIRQ_HARDWARE)
{
LogFlow(("apic: clear interrupt flag\n"));
getCpuFromLapic(pDev, s));
}
# ifdef IN_RING3
{
getCpuFromLapic(pDev, s),
vector);
}
{
Log2(("apic: send init IPI\n"));
getCpuFromLapic(pDev, s));
}
# endif /* IN_RING3 */
{
switch (pDev->enmVersion)
{
case PDMAPICVERSION_NONE:
return 0;
case PDMAPICVERSION_APIC:
return MSR_IA32_APICBASE_ENABLE;
case PDMAPICVERSION_X2APIC:
default:
return 0;
}
}
{
{
case 0:
return PDMAPICVERSION_NONE;
case 1:
default:
/* Invalid */
return PDMAPICVERSION_NONE;
case 2:
return PDMAPICVERSION_APIC;
case 3:
return PDMAPICVERSION_X2APIC;
}
}
{
LogFlow(("apic_bus_deliver mask=%R[vmcpuset] mode=%x vector=%x polarity=%x trigger_mode=%x uTagSrc=%#x\n",
switch (delivery_mode)
{
case APIC_DM_LOWPRI:
{
if (idDstCpu != NIL_VMCPUID)
{
}
return VINF_SUCCESS;
}
case APIC_DM_FIXED:
/** @todo XXX: arbitration */
break;
case APIC_DM_SMI:
return VINF_SUCCESS;
case APIC_DM_NMI:
return VINF_SUCCESS;
case APIC_DM_INIT:
/* normal INIT IPI sent to processors */
#ifdef IN_RING3
return VINF_SUCCESS;
#else
/* We shall send init IPI only in R3. */
return VINF_IOM_R3_MMIO_READ_WRITE;
#endif /* IN_RING3 */
case APIC_DM_EXTINT:
/* handled in I/O APIC code */
break;
default:
return VINF_SUCCESS;
}
return VINF_SUCCESS;
}
{
/** @todo: do we need to lock here ? */
/* APIC_LOCK_VOID(pDev, VERR_INTERNAL_ERROR); */
/** @todo If this change is valid immediately, then we should change the MMIO registration! */
/* We cannot change if this CPU is BSP or not by writing to MSR - it's hardwired */
s->apicbase =
{
switch (newMode)
{
case PDMAPICVERSION_NONE:
{
s->spurious_vec &= ~APIC_SV_ENABLE;
/* Clear any pending APIC interrupt action flag. */
cpuClearInterrupt(pDev, s);
/** @todo: why do we do that? */
break;
}
case PDMAPICVERSION_APIC:
/** @todo: map MMIO ranges, if needed */
break;
case PDMAPICVERSION_X2APIC:
/** @todo: unmap MMIO ranges of this APIC, according to the spec */
break;
default:
break;
}
}
/* APIC_UNLOCK(pDev); */
}
{
return s->apicbase;
}
{
}
{
return s->tpr;
}
/**
* apicWriteRegister helper for dealing with invalid register access.
*
* @returns Strict VBox status code.
* @param pDev The PDM device instance.
* @param pApic The APIC being written to.
* @param iReg The APIC register index.
* @param u64Value The value being written.
* @param rcBusy The busy return code to employ. See
* PDMCritSectEnter for a description.
* @param fMsr Set if called via MSR, clear if MMIO.
*/
static int apicWriteRegisterInvalid(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t u64Value,
{
Log(("apicWriteRegisterInvalid/%u: iReg=%#x fMsr=%RTbool u64Value=%#llx\n", pApic->phys_id, iReg, fMsr, u64Value));
return rc;
}
/**
* Writes to an APIC register via MMIO or MSR.
*
* @returns Strict VBox status code.
* @param pDev The PDM device instance.
* @param pApic The APIC being written to.
* @param iReg The APIC register index.
* @param u64Value The value being written.
* @param rcBusy The busy return code to employ. See
* PDMCritSectEnter for a description.
* @param fMsr Set if called via MSR, clear if MMIO.
*/
static int apicWriteRegister(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t u64Value,
{
int rc = VINF_SUCCESS;
switch (iReg)
{
case 0x02:
break;
case 0x03:
/* read only, ignore write. */
break;
case 0x08:
break;
case 0x09: case 0x0a:
break;
case 0x0b: /* EOI */
break;
case 0x0d:
break;
case 0x0e:
pApic->dest_mode = u64Value >> 28; /** @todo r=bird: range? This used to be 32-bit before morphed into an MSR handler. */
break;
case 0x0f:
break;
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28:
break;
case 0x30:
if (fMsr) /* Here one of the differences with regular APIC: ICR is single 64-bit register */
break;
case 0x31:
if (!fMsr)
{
}
else
break;
case 0x32 + APIC_LVT_TIMER:
AssertCompile(APIC_LVT_TIMER == 0);
break;
case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
break;
case 0x38:
break;
case 0x39:
break;
case 0x3e:
{
break;
}
case 0x3f:
if (fMsr)
{
/* Self IPI, see x2APIC book 2.4.5 */
&SelfSet,
0 /* Delivery mode - fixed */,
0 /* Polarity - conform to the bus */,
0 /* Trigger mode - edge */,
break;
}
/* else: fall thru */
default:
break;
}
return rc;
}
/**
* apicReadRegister helper for dealing with invalid register access.
*
* @returns Strict VBox status code.
* @param pDev The PDM device instance.
* @param pApic The APIC being read to.
* @param iReg The APIC register index.
* @param pu64Value Where to store the value we've read.
* @param rcBusy The busy return code to employ. See
* PDMCritSectEnter for a description.
* @param fMsr Set if called via MSR, clear if MMIO.
*/
static int apicReadRegisterInvalid(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t *pu64Value,
{
*pu64Value = 0;
return rc;
}
/**
* Read from an APIC register via MMIO or MSR.
*
* @returns Strict VBox status code.
* @param pDev The PDM device instance.
* @param pApic The APIC being read to.
* @param iReg The APIC register index.
* @param pu64Value Where to store the value we've read.
* @param rcBusy The busy return code to employ. See
* PDMCritSectEnter for a description.
* @param fMsr Set if called via MSR, clear if MMIO.
*/
static int apicReadRegister(APICDeviceInfo *pDev, APICState *pApic, uint32_t iReg, uint64_t *pu64Value,
{
int rc = VINF_SUCCESS;
switch (iReg)
{
case 0x02: /* id */
break;
case 0x03: /* version */
#if 0
| (0 << 24) /* Support for EOI broadcast suppression */
#endif
;
break;
case 0x08:
break;
case 0x09:
break;
case 0x0a:
/* ppr */
break;
case 0x0b:
*pu64Value = 0;
break;
case 0x0d:
break;
case 0x0e:
/* Bottom 28 bits are always 1 */
break;
case 0x0f:
break;
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
break;
case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
break;
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
break;
case 0x28:
break;
case 0x30:
/* Here one of the differences with regular APIC: ICR is single 64-bit register */
if (fMsr)
else
break;
case 0x31:
if (fMsr)
else
{
}
break;
case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
break;
case 0x38:
break;
case 0x39:
break;
case 0x3e:
break;
case 0x3f:
if (fMsr)
{
/* Self IPI register is write only */
*pu64Value = 0;
}
else
break;
case 0x2f: /** @todo Correctable machine check exception vector, implement me! */
default:
/**
* @todo: according to spec when APIC writes to ESR it msut raise error interrupt,
* i.e. LVT[5]
*/
break;
}
return rc;
}
/**
* @interface_method_impl{PDMAPICREG,pfnWriteMSRR3}
*/
PDMBOTHCBDECL(int) apicWriteMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t u64Value)
{
return VERR_EM_INTERPRETER; /** @todo tell the caller to raise hell (\#GP(0)). */
}
/**
* @interface_method_impl{PDMAPICREG,pfnReadMSRR3}
*/
PDMBOTHCBDECL(int) apicReadMSR(PPDMDEVINS pDevIns, VMCPUID idCpu, uint32_t u32Reg, uint64_t *pu64Value)
{
return VERR_EM_INTERPRETER;
}
/**
* More or less private interface between IOAPIC, only PDM is responsible
* for connecting the two devices.
*/
{
LogFlow(("apicBusDeliverCallback: pDevIns=%p u8Dest=%#x u8DestMode=%#x u8DeliveryMode=%#x iVector=%#x u8Polarity=%#x u8TriggerMode=%#x uTagSrc=%#x\n",
}
/**
* Normally used for 8259A PIC and NMI.
*/
{
/* If LAPIC is disabled, go straight to the CPU. */
if (!(s->spurious_vec & APIC_SV_ENABLE))
{
LogFlow(("apicLocalInterrupt: LAPIC disabled, delivering directly to CPU core.\n"));
if (u8Level)
else
return VINF_SUCCESS;
}
/* If LAPIC is enabled, interrupts are subject to LVT programming. */
/* There are only two local interrupt pins. */
/* NB: We currently only deliver local interrupts to the first CPU. In theory they
* should be delivered to all CPUs and it is the guest's responsibility to ensure
* no more than one CPU has the interrupt unmasked.
*/
/* Drop int if entry is masked. May not be correct for level-triggered interrupts. */
if (!(u32Lvec & APIC_LVT_MASKED))
switch (u8Delivery)
{
case APIC_DM_EXTINT:
if (u8Level)
else
return VINF_SUCCESS;
case APIC_DM_NMI:
/* External NMI should be wired to LINT1, but Linux sometimes programs
* LVT0 to NMI delivery mode as well.
*/
/* Currently delivering NMIs through here causes problems with NMI watchdogs
* on certain Linux kernels, e.g. 64-bit CentOS 5.3. Disable NMIs for now.
*/
return VINF_SUCCESS;
case APIC_DM_SMI:
break;
case APIC_DM_FIXED:
{
/** @todo implement APIC_DM_FIXED! */
static unsigned s_c = 0;
if (s_c++ < 5)
return VINF_SUCCESS;
}
case APIC_DM_INIT:
/** @todo implement APIC_DM_INIT? */
default:
{
static unsigned s_c = 0;
if (s_c++ < 100)
AssertLogRelMsgFailed(("delivery type %d not implemented. u8Pin=%d u8Level=%d\n", u8Delivery, u8Pin, u8Level));
return VERR_INTERNAL_ERROR_4;
}
}
}
return VINF_SUCCESS;
}
static int apic_get_ppr(APICState const *s)
{
int ppr;
isrv >>= 4;
else
return ppr;
}
static int apic_get_ppr_zero_tpr(APICState *s)
{
return Apic256BitReg_FindLastSetBit(&s->isr, 0);
}
static int apic_get_arb_pri(APICState const *s)
{
/** @todo XXX: arbitration */
return 0;
}
/* signal the CPU if an irq is pending */
{
if (!(s->spurious_vec & APIC_SV_ENABLE))
{
/* Clear any pending APIC interrupt action flag. */
cpuClearInterrupt(pDev, s);
return false;
}
if (irrv < 0)
return false;
int ppr = apic_get_ppr(s);
return false;
cpuSetInterrupt(pDev, s);
return true;
}
{
if (!pDev)
return false;
/*
* All our callbacks now come from single IOAPIC, thus locking
* seems to be excessive now
*/
/** @todo check excessive locking whatever... */
if (irrv < 0)
return false;
int ppr = apic_get_ppr_zero_tpr(s);
return false;
return true;
}
{
bool fIrqIsActive = false;
bool fIrqWasActive = false;
/* If an interrupt is pending and now masked, then clear the FF flag. */
if (fIrqWasActive && !fIrqIsActive)
{
cpuClearInterrupt(pDev, s);
}
}
static void apic_set_irq(APICDeviceInfo *pDev, APICState* s, int vector_num, int trigger_mode, uint32_t uTagSrc)
{
LogFlow(("CPU%d: apic_set_irq vector=%x trigger_mode=%x uTagSrc=%#x\n", s->phys_id, vector_num, trigger_mode, uTagSrc));
if (trigger_mode)
else
if (!s->auTags[vector_num])
else
apic_update_irq(pDev, s);
}
{
if (isrv < 0)
return;
/** @todo XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
* set the remote IRR bit for level triggered interrupts. */
apic_update_irq(pDev, s);
}
static PVMCPUSET apic_get_delivery_bitmask(APICDeviceInfo *pDev, uint8_t dest, uint8_t dest_mode, PVMCPUSET pDstSet)
{
if (dest_mode == 0)
{
else
}
else
{
/** @todo XXX: cluster mode */
{
}
{
}
}
return pDstSet;
}
#ifdef IN_RING3
{
int i;
for(i = 0; i < APIC_LVT_NB; i++)
s->tpr = 0;
s->spurious_vec = 0xff;
s->log_dest = 0;
Apic256BitReg_Empty(&s->isr);
Apic256BitReg_Empty(&s->tmr);
Apic256BitReg_Empty(&s->irr);
s->esr = 0;
s->divide_conf = 0;
s->count_shift = 1;
s->initial_count = 0;
s->initial_count_load_time = 0;
s->next_time = 0;
}
{
apic_init_ipi(pDev, s);
cpuSendInitIpi(pDev, s);
}
/* send a SIPI message to the CPU to start it */
{
}
#endif /* IN_RING3 */
{
LogFlow(("apic_deliver dest=%x dest_mode=%x dest_shorthand=%x delivery_mode=%x vector_num=%x polarity=%x trigger_mode=%x uTagSrc=%#x\n", dest, dest_mode, dest_shorthand, delivery_mode, vector_num, polarity, trigger_mode));
switch (dest_shorthand)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
switch (delivery_mode)
{
case APIC_DM_INIT:
{
{
return VINF_SUCCESS;
}
break;
}
case APIC_DM_SIPI:
# ifdef IN_RING3
return VINF_SUCCESS;
# else
/* We shall send SIPI only in R3, R0 calls should be
rescheduled to R3 */
return VINF_IOM_R3_MMIO_WRITE;
# endif
}
}
{
/* if the APIC is not installed or enabled, we let the 8259 handle the
IRQs */
if (!pDev)
{
Log(("apic_get_interrupt: returns -1 (!s)\n"));
return -1;
}
if (!(s->spurious_vec & APIC_SV_ENABLE))
{
return -1;
}
/** @todo XXX: spurious IRQ handling */
if (intno < 0)
{
return -1;
}
{
*puTagSrc = 0;
return s->spurious_vec & 0xff;
}
apic_update_irq(pDev, s);
return intno;
}
/**
* @remarks Caller (apicReadRegister) takes both the TM and APIC locks before
* calling this function.
*/
{
>> pApic->count_shift;
/* periodic */
else if (d >= pApic->initial_count)
val = 0;
else
return val;
}
/**
* Does the frequency hinting and logging.
*
* @param pApic The device state.
*/
{
{
if (pApic->initial_count > 0)
{
}
else
uHz = 0;
}
}
/**
* Implementation of the 0380h access: Timer reset + new initial count.
*
* @param pDev The device state.
* @param pApic The APIC sub-device state.
* @param u32NewInitialCount The new initial count for the timer.
*/
static void apicTimerSetInitialCount(APICDeviceInfo *pDev, APICState *pApic, uint32_t u32NewInitialCount)
{
/*
* Don't (re-)arm the timer if the it's masked or if it's
* a zero length one-shot timer.
*/
&& u32NewInitialCount > 0)
{
/*
* operation. This avoids racing the clock between get and set.
*/
cTicksNext += 1;
pApic->fTimerArmed = true;
Log(("apicTimerSetInitialCount: cTicksNext=%'llu (%#llx) ic=%#x sh=%#x nxt=%#llx\n",
}
else
{
/* Stop it if necessary and record the load time for unmasking. */
if (pApic->fTimerArmed)
{
pApic->fTimerArmed = false;
}
Log(("apicTimerSetInitialCount: ic=%#x sh=%#x iclt=%#llx\n", u32NewInitialCount, pApic->count_shift, pApic->initial_count_load_time));
}
}
/**
* Implementation of the 0320h access: change the LVT flags.
*
* @param pDev The device state.
* @param pApic The APIC sub-device state to operate on.
* @param fNew The new flags.
*/
{
/*
* Make the flag change, saving the old ones so we can avoid
* unnecessary work.
*/
/* Only the masked and peridic bits are relevant (see apic_timer_update). */
{
/*
* If changed to one-shot from periodic, stop the timer if we're not
* in the first period.
*/
/** @todo check how clearing the periodic flag really should behave when not
* in period 1. The current code just mirrors the behavior of the
* original implementation. */
if ( (fOld & APIC_LVT_TIMER_PERIODIC)
&& !(fNew & APIC_LVT_TIMER_PERIODIC))
{
{
/* not first period, stop it. */
pApic->fTimerArmed = false;
}
/* else: first period, let it fire normally. */
}
/*
* We postpone stopping the timer when it's masked, this way we can
* avoid some timer work when the guest temporarily masks the timer.
* (apicR3TimerCallback will stop it if still masked.)
*/
if (fNew & APIC_LVT_MASKED)
else if (pApic->fTimerArmed)
/*
* If unmasked, not armed and with a valid initial count value (according
* to our interpretation of the spec), we will have to rearm the timer so
* it will fire at the end of the current period.
*
* N.B. This is code is currently RACING the virtual sync clock!
*/
else if ( (fOld & APIC_LVT_MASKED)
&& pApic->initial_count > 0)
{
{
uint64_t cTicks = (TMTimerGet(pApic->CTX_SUFF(pTimer)) - pApic->initial_count_load_time) >> pApic->count_shift;
if (fNew & APIC_LVT_TIMER_PERIODIC)
NextTS = ((cTicks / ((uint64_t)pApic->initial_count + 1)) + 1) * ((uint64_t)pApic->initial_count + 1);
else
{
break;
}
/* Try avoid the assertion in TM.cpp... this isn't perfect! */
|| cTries > 10)
{
pApic->fTimerArmed = true;
Log(("apicTimerSetLvt: ic=%#x sh=%#x nxt=%#llx\n", pApic->initial_count, pApic->count_shift, pApic->next_time));
break;
}
}
}
}
else
}
# ifdef IN_RING3
/**
* Timer callback function.
*
* @param pDevIns The device state.
* @param pTimer The timer handle.
* @param pvUser User argument pointing to the APIC instance.
*/
{
LogFlow(("apic_timer: trigger irq\n"));
&& pApic->initial_count > 0) {
/* new interval. */
pApic->fTimerArmed = true;
Log2(("apicR3TimerCallback: ic=%#x sh=%#x nxt=%#llx\n", pApic->initial_count, pApic->count_shift, pApic->next_time));
} else {
/* single shot or disabled. */
pApic->fTimerArmed = false;
}
} else {
/* masked, do not rearm. */
pApic->fTimerArmed = false;
}
}
{
int i;
SSMR3PutU32(f, s->apicbase);
SSMR3PutU32(f, s->id);
SSMR3PutU32(f, s->phys_id);
SSMR3PutU32(f, s->arb_id);
SSMR3PutU32(f, s->tpr);
SSMR3PutU32(f, s->spurious_vec);
SSMR3PutU8(f, s->log_dest);
SSMR3PutU8(f, s->dest_mode);
for (i = 0; i < 8; i++) {
}
for (i = 0; i < APIC_LVT_NB; i++) {
SSMR3PutU32(f, s->lvt[i]);
}
SSMR3PutU32(f, s->esr);
SSMR3PutU32(f, s->icr[0]);
SSMR3PutU32(f, s->divide_conf);
SSMR3PutU32(f, s->count_shift);
SSMR3PutU32(f, s->initial_count);
SSMR3PutU64(f, s->initial_count_load_time);
SSMR3PutU64(f, s->next_time);
}
{
int i;
/** @todo XXX: what if the base changes? (registered memory regions) */
SSMR3GetU32(f, &s->apicbase);
switch (version_id)
{
{
SSMR3GetU8(f, &val);
/* UP only in old saved states */
s->phys_id = 0;
SSMR3GetU8(f, &val);
break;
}
case APIC_SAVED_STATE_VERSION:
SSMR3GetU32(f, &s->id);
SSMR3GetU32(f, &s->phys_id);
SSMR3GetU32(f, &s->arb_id);
break;
default:
}
SSMR3GetU32(f, &s->tpr);
SSMR3GetU32(f, &s->spurious_vec);
SSMR3GetU8(f, &s->log_dest);
SSMR3GetU8(f, &s->dest_mode);
for (i = 0; i < 8; i++) {
}
for (i = 0; i < APIC_LVT_NB; i++) {
SSMR3GetU32(f, &s->lvt[i]);
}
SSMR3GetU32(f, &s->esr);
SSMR3GetU32(f, &s->icr[0]);
SSMR3GetU32(f, &s->divide_conf);
s->uHintedCountShift = s->uHintedInitialCount = 0;
if (s->fTimerArmed)
return VINF_SUCCESS; /** @todo darn mess! */
}
#endif /* IN_RING3 */
/* LAPIC */
PDMBOTHCBDECL(int) apicMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
{
/** @todo add LAPIC range validity checks (different LAPICs can
* theoretically have different physical addresses, see @bugref{3092}) */
switch (cb)
{
case 1:
/** @todo this is not how recent APIC behave! We will fix
* this via the IOM. */
break;
case 2:
/** @todo this is not how recent APIC behave! */
break;
case 4:
{
#if 0 /** @note experimental */
#ifndef IN_RING3
&& ++s->cTPRPatchAttempts < APIC_MAX_PATCH_ATTEMPTS)
{
#ifdef IN_RC
#else
pDevIns->pHlpR0->pfnPATMSetMMIOPatchInfo(pDevIns, GCPhysAddr, pDevIns + RT_OFFSETOF(APICState, tpr));
#endif
return VINF_PATM_HC_MMIO_PATCH_READ;
}
#endif
#endif /* experimental */
/* It does its own locking. */
VINF_IOM_R3_MMIO_READ, false /*fMsr*/);
return rc;
}
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
PDMBOTHCBDECL(int) apicMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
{
/** @todo: add LAPIC range validity checks (multiple LAPICs can theoretically have
* different physical addresses, see @bugref{3092}) */
switch (cb)
{
case 1:
case 2:
/* ignore */
break;
case 4:
/* It does its own locking. */
VINF_IOM_R3_MMIO_WRITE, false /*fMsr*/);
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
#ifdef IN_RING3
/**
* Wrapper around apicReadRegister.
*
* @returns 64-bit register value.
* @param pDev The PDM device instance.
* @param pApic The Local APIC in question.
* @param iReg The APIC register index.
*/
{
return u64Value;
}
/**
* Print a 8-DWORD Local APIC bit map (256 bits).
*
* @param pDev The PDM device instance.
* @param pApic The Local APIC in question.
* @param pHlp The output helper.
* @param iStartReg The register to start at.
*/
static void apicR3DumpVec(APICDeviceInfo *pDev, APICState *pApic, PCDBGFINFOHLP pHlp, uint32_t iStartReg)
{
for (uint32_t i = 0; i < 8; i++)
}
/**
* Print basic Local APIC state.
*
* @param pDev The PDM device instance.
* @param pApic The Local APIC in question.
* @param pHlp The output helper.
*/
{
}
/**
* Print the more interesting Local APIC LVT entries.
*
* @param pDev The PDM device instance.
* @param pApic The Local APIC in question.
* @param pHlp The output helper.
*/
{
static const char * const s_apszDeliveryModes[] =
{
"Fixed ", "Reserved", "SMI", "Reserved", "NMI", "INIT", "Reserved", "ExtINT"
};
}
/**
* Print LAPIC timer state.
*
* @param pDev The PDM device instance.
* @param pApic The Local APIC in question.
* @param pHlp The output helper.
*/
{
}
/**
* @callback_method_impl{FNDBGFHANDLERDEV,
* Dumps the Local APIC state according to given argument.}
*/
{
else
}
/**
* @copydoc FNSSMDEVLIVEEXEC
*/
{
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* @copydoc FNSSMDEVSAVEEXEC
*/
{
/* config */
/* save all APICs data */ /** @todo: is it correct? */
return VINF_SUCCESS;
}
/**
* @copydoc FNSSMDEVLOADEXEC
*/
static DECLCALLBACK(int) apicR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
if ( uVersion != APIC_SAVED_STATE_VERSION
/* config */
{
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - cCpus: saved=%#x config=%#x"), cCpus, pDev->cCpus);
bool fIoApic;
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - fIoApic: saved=%RTbool config=%RTbool"), fIoApic, pDev->fIoApic);
return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch - uApicVersion: saved=%#x config=%#x"), uApicVersion, pDev->enmVersion);
}
if (uPass != SSM_PASS_FINAL)
return VINF_SUCCESS;
/* load all APICs data */ /** @todo: is it correct? */
int rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
break;
return rc;
}
/**
* @copydoc FNPDMDEVRESET
*/
{
/* Reset all APICs. */
{
/* Clear LAPIC state as if an INIT IPI was sent. */
/* The IDs are not touched by apic_init_ipi() and must be reset now. */
/* Reset should re-enable the APIC, see comment in msi.h */
/* Clear any pending APIC interrupt action flag. */
}
/** @todo r=bird: Why is this done everytime, while the constructor first
* checks the CPUID? Who is right? */
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/**
* Initializes the state of one local APIC.
*
* @param pApic The Local APIC state to init.
* @param id The Local APIC ID.
*/
{
/* See comment in msi.h for LAPIC base info. */
if (id == 0) /* Mark first CPU as BSP. */
for (int i = 0; i < APIC_LVT_NB; i++)
}
/**
* @copydoc FNPDMDEVCONSTRUCT
*/
{
uint32_t i;
/*
* Only single device instance.
*/
/*
* Validate configuration.
*/
bool fIoApic;
if (RT_FAILURE(rc))
N_("Configuration error: Failed to read \"IOAPIC\""));
bool fRZEnabled;
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query boolean value \"RZEnabled\""));
if (RT_FAILURE(rc))
N_("Configuration error: Failed to query integer value \"NumCPUs\""));
if (cCpus > 255)
N_("Configuration error: Invalid value for \"NumCPUs\""));
/*
* Init the data.
*/
/* Use PDMAPICVERSION_X2APIC to activate x2APIC mode */
/* Disable locking in this device. */
/*
* We are not freeing this memory, as it's automatically released when guest exits.
*/
rc = MMHyperAlloc(pVM, cCpus * sizeof(APICState), 1, MM_TAG_PDM_DEVICE_USER, (void **)&pDev->paLapicsR3);
if (RT_FAILURE(rc))
return VERR_NO_MEMORY;
for (i = 0; i < cCpus; i++)
/*
* Register the APIC.
*/
if (fRZEnabled)
{
}
else
{
}
/*
* The CPUID feature bit.
*/
/** @todo r=bird: See remark in the apicR3Reset. */
if (u32Eax >= 1)
{
if ( fIoApic /* If IOAPIC is enabled, enable Local APIC in any case */
|| ( u32Ebx == X86_CPUID_VENDOR_INTEL_EBX
|| ( u32Ebx == X86_CPUID_VENDOR_AMD_EBX
{
LogRel(("Activating Local APIC\n"));
}
}
/*
* Register the MMIO range.
*/
/** @todo: shall reregister, if base changes. */
if (RT_FAILURE(rc))
return rc;
if (fRZEnabled)
{
rc = PDMDevHlpMMIORegisterRC(pDevIns, ApicBase, 0x1000, NIL_RTRCPTR /*pvUser*/, "apicMMIOWrite", "apicMMIORead");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpMMIORegisterR0(pDevIns, ApicBase, 0x1000, NIL_RTR0PTR /*pvUser*/, "apicMMIOWrite", "apicMMIORead");
if (RT_FAILURE(rc))
return rc;
}
/*
* Create the APIC timers.
*/
for (i = 0; i < cCpus; i++)
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Saved state.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Register debugger info callback.
*/
"Recognizes 'basic', 'lvt', 'timer' as arguments, defaulting to 'basic'.", apicR3Info);
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOReadGC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOReadGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOReadHC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOReadHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO reads in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOWriteGC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOWriteGC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in GC.");
PDMDevHlpSTAMRegister(pDevIns, &pDev->StatMMIOWriteHC, STAMTYPE_COUNTER, "/Devices/APIC/MMIOWriteHC", STAMUNIT_OCCURENCES, "Number of APIC MMIO writes in HC.");
PDMDevHlpSTAMRegister(pDevIns, &pDev->StatClearedActiveIrq,STAMTYPE_COUNTER, "/Devices/APIC/MaskedActiveIRQ", STAMUNIT_OCCURENCES, "Number of cleared irqs.");
for (i = 0; i < cCpus; i++)
{
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCount, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Calls to apicTimerSetInitialCount.", "/Devices/APIC/%u/TimerSetInitialCount", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCountArm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSetRelative calls.", "/Devices/APIC/%u/TimerSetInitialCount/Arm", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetInitialCountDisarm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerStop calls.", "/Devices/APIC/%u/TimerSetInitialCount/Disasm", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Calls to apicTimerSetLvt.", "/Devices/APIC/%u/TimerSetLvt", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtClearPeriodic, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Clearing APIC_LVT_TIMER_PERIODIC.", "/Devices/APIC/%u/TimerSetLvt/ClearPeriodic", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtPostponed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerStop postponed.", "/Devices/APIC/%u/TimerSetLvt/Postponed", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArmed, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet avoided.", "/Devices/APIC/%u/TimerSetLvt/Armed", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArm, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet necessary.", "/Devices/APIC/%u/TimerSetLvt/Arm", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtArmRetries, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "TMTimerSet retries.", "/Devices/APIC/%u/TimerSetLvt/ArmRetries", i);
PDMDevHlpSTAMRegisterF(pDevIns, &pApic->StatTimerSetLvtNoRelevantChange,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "No relevant flags changed.", "/Devices/APIC/%u/TimerSetLvt/NoRelevantChange", i);
}
#endif
return VINF_SUCCESS;
}
/**
* APIC device registration structure.
*/
const PDMDEVREG g_DeviceAPIC =
{
/* u32Version */
/* szName */
"apic",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"Advanced Programmable Interrupt Controller (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(APICState),
/* 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 */